Validate a Textbox in ASP .NET to accept the below input without using validation controls
1. Accepting only numeric input
2. Accepting only characters.
3. Accepting alphanumeric input.
Note: Provide clear explanation with comments which is used for what purpose.
Task1: First use without using javascirpt
Task 2: With Javascript achieve the solution for the above

Srinivas PabballaPosted Aug 24, 2015, 7:45 AM
Srinivas PabballaPosted Aug 24, 2015, 7:45 AM
Munesh SharmaPosted Aug 20, 2015, 7:02 AM
Rajeesh MenothPosted Aug 20, 2015, 7:02 AM
Only letters:
Use the expression:
ie:
Munesh SharmaPosted Aug 20, 2015, 7:01 AM
//Only numeric
private void txtHomePhone_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The character represents a backspace
{
e.Handled = false; //Do not reject the input
}
else
{
e.Handled = true; //Reject the input
}
}
//
TextBoxonly accept alphabetic charactersprivate void textBox1_TextChanged(object sender, EventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]"))
{
MessageBox.Show("This textbox accepts only alphabetical characters");
textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}