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


Munesh SharmaRank #174 · 13.9K reputationPosted Aug 20, 2015, 3:01 AM EDT
//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);
}
}
Rajeesh MenothRank #118 · 18.4K reputationPosted Aug 20, 2015, 3:02 AM EDT
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar); }Only letters:
Use the expression:
ie:
Munesh SharmaRank #174 · 13.9K reputationPosted Aug 20, 2015, 3:02 AM EDT
Srinivas PabballaRank #5,010 · 315 reputationPosted Aug 24, 2015, 3:45 AM EDT
Srinivas PabballaRank #5,010 · 315 reputationPosted Aug 24, 2015, 3:45 AM EDT