-
June 20th, 2010, 04:41 AM
#1
How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...)
Hi guys,
I just wonder how i can validate a TextBox( like txtName) to accept just string value and not any numeric value?
Cloud u please let me know how I can do it? should I use a try and catch block?
Thanks
-
June 20th, 2010, 04:10 PM
#2
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
No, for the love of God, don't use a try/catch block to control the flow of your program
This is easily done one of two ways.
1. Use a MaskedTextBox if you need a specific format.
2. Handle the KeyDown event and check the input. If it is numeric, set e.Handled to true and nothing will appear in the TextBox.
Last edited by BigEd781; June 20th, 2010 at 10:21 PM.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
June 20th, 2010, 08:57 PM
#3
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
In this case you would want to set e.handled to true if it is numeric since the goal is to not allow numeric characters. If you are going for names then you may also want to trap most other characters and allow only A-z as well as backspace.
Always use [code][/code] tags when posting code.
-
June 20th, 2010, 10:22 PM
#4
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
yes, data is right. I must have read the first post backward.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
June 21st, 2010, 04:15 AM
#5
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
You may use the following code to validate,
Code:
//Keypress event of your textbox
private void txtTarget_KeyPress(object sender, KeyPressEventArgs e)
{
int x = (int)e.KeyChar;
e.Handled = !ValidateAlpha(x);
}
//Validation function
public static bool ValidateAlpha(int keyAscii)
{
if (keyAscii >= 65 && keyAscii <= 90)
return true;
else if (keyAscii >= 97 && keyAscii <= 122)
return true;
else if (keyAscii == 8 || keyAscii == 13)
return true;
return false;
}
-
June 21st, 2010, 05:03 AM
#6
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
There is a .net regular expression validator control to use in asp.net.
-
July 13th, 2010, 08:17 PM
#7
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
Originally Posted by Alsvha
There is a .net regular expression validator control to use in asp.net.
Iam sure the code above would have helped you in ASP.net or C#. Why dont you reply or atleast mark it as resolved?
Regards,
CT.
Check out www.cuteassistant.com - Manage your tasks, take notes, securely store data and more!
-
July 14th, 2010, 05:49 PM
#8
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
KeyDOwn event should work. You can check for an ascii range, else set e.cancel = true, so the key will not be processed. It works well.
www.cuteassistant.com - Increase your productivity! Be more successful in life!
-
July 14th, 2010, 11:18 PM
#9
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
A simple textbox to handle this.
public class TextOnlyBox:TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
//Mark the event as handled, the base method won't process this.
e.Handled = (char.IsNumber(e.KeyChar));
base.OnKeyPress(e);
}
protected override void OnValidating(CancelEventArgs e)
{
//Validation is required, as someone can copy paste the data
//I even override the Text property to format the values
//Remove any number, if there
char[] CharsInTheText = this.Text.ToCharArray();
//use a stringbuilder, it is very efficient for appeding string
StringBuilder StringBuilder = new StringBuilder(CharsInTheText.Length, CharsInTheText.Length);
foreach (char CharInTheText in CharsInTheText)
{
if (! char.IsNumber(CharInTheText))
{
StringBuilder.Append(CharInTheText);
}
}
Text = StringBuilder.ToString();
base.OnValidating(e);
}
}
Last edited by partho; July 15th, 2010 at 12:28 AM.
Reason: found a bug in my code sample, corrected it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|