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
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.
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.
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.
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;
}
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.
Re: How to Validate a TextBox to accept only String - Not any numeric(int ,decimal...
Quote:
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!
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!
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);
}
}