How to validate textbox value while user enters and display error message?

textbox validation in c#
how to validate a textbox in c# to accept only numeric value

So, I have to code for a method that validates whether the string that saves name contains alphabets only, no numbers. The validation of textbox values should apply when the user enters by textchanged event before submitting the form and display an error message of red color on the label. My code works but the problem is when I enter a numeric number in text box, the label displays error which stays even when I delete the text box value and enter the alphabetic string.

I have declared a method which assign error string to label, and is called if regular expression does not match with the text box input, during text changed event.

public void Validator()
{
    Calculate_Salary.Enabled = false;
    label4.Text = "Please enter only alphabetical letters";
}

private void _Name_TextChanged(object sender, EventArgs e)
{
    Regex pattern = new Regex("/^[A-Za-z]+$/");
    string name = _Name.Text;
    if (pattern.IsMatch(name))
    {
        Calculate_Salary.Enabled = true;
        label4.Text = "";

    }
    else
    {
        Validator();
    }
}     

Your Regex comparison is wrong try this code:

public void Validator()
{
    Calculate_Salary.Enabled = false;
    label4.Text = "Please enter only alphabetical letters";
}

private void _Name_TextChanged(object sender, EventArgs e)
{
    label4.Text = "";
    string name = _Name.Text;
    if (Regex.IsMatch(name, @"^[a-zA-Z]+$"))
        Calculate_Salary.Enabled = true;
    else
        Validator();
}

Validate user input using errors and warnings, Enter the regular expression in the Regular expression: text field. Enter a message in the Error message: text field, to be displayed to the user when the validation is triggered, or leave the default error message ("Invalid Value"). Click OK. If you set the ValidationText property, the text that you enter is displayed as the error message. For example, when a record is added for a new employee, you can enter a ValidationRule property requiring that the value in the employee's StartDate field fall between the company's founding date and the current date.

Just clear the textbox before you validate:

public void Validator()
{
    Calculate_Salary.Enabled = false;
    label4.Text = "Please enter only alphabetical letters";
}
private void _Name_TextChanged(object sender, EventArgs e)
{
    label4.Text = "";
    Regex pattern = new Regex("/^[A-Za-z]+$/");
    string name = _Name.Text;
    if (pattern.IsMatch(name))
    {
        Calculate_Salary.Enabled = true;

    }
    else
    {
        Validator();
    }
}

Validating Text and Numeric Fields – Liferay Help Center, Validation ensures that only certain values are entered in a field. Validation Add a Text or Numeric field to a form in the Builder view. Open the field's If the condition isn't met, an error message is displayed to the user. Where you Figure 3: Use text field validation to make sure users enter a valid email address or URL. If we want user to enter number between 1 & 10 then the user will not be able to enter any other number like -1, 0, 11 & so on. To prevent the user from entering numbers other than between 1 & 10, we will use Data Validation Error Alert. Select the cell in which you want Data Validation to be applied. Click on Data tab

I changed the validation code. It seems to work now.

private void _Name_TextChanged(object sender, EventArgs e)
{
    label4.Text = string.Empty;

    string name = _Name.Text;
    if (Regex.IsMatch(_Name.Text, "^[a-zA-Z]+$") || _Name.Text=="")
    {
        Calculate_Salary.Enabled = true;
    }
    else
    {
        Calculate_Salary.Enabled = false;
        label4.Text = Validator();
    }
}

Validating Input • Forms • WAI Web Accessibility Tutorials, In addition to providing instructions, validate user input to help users avoid mistakes. Instead, it will display a message that is generated by the web browser itself. Most current web browsers automatically set its value to true when the Input errors, such as an incorrect email address, will be indicated using the web� When I write like that only first textbox is validating. When I remove "retun false" 2nd text box is also validating. When I write another validation for first textbox only second second validation is working. I am doing Gmail Registration page validations with JavaScript. Please help me how to do it . Thanks in Advance. Kiran

Validating User Input – An Introduction, Validation occurs when a user clicks any Button control by default, but you we can see the RequiredFieldValidator error message display on the form. Use the CompareValidator to compare the value of an input control to� If an element does not verify change the invalid $_POST value to the error message, then use those $_POST values to build the next form. If all elements verify, then send the email. If $_POST is not set, then display the form as usual because we know that the user has not entered any data yet.

Validating User Input, Some fields may require the user to enter only alphabetic values but no use of the ErrorProvider component to display error messages to the user. When you use the Validating event to restrict the focus in the control by� Server-side validation This validation is executed after client-side validation if we have it. But we should or must have server-side validation. A user or hacker can submit the data through different channels also. Server-side validation comes into the picture/existence while the user submits or posts back. Client Side Validation Code

Client-side form validation, Note how the invalid input gets focus, a default error message ("Please Note: For good user experience, indicate to the user when form fields are required. Specifying the email type, for example, validates the inputs value against Entered numbers outside this range will show as invalid; users won't be� 1.3.Get the length of textbox input To get the length of textbox input, we have to use the predefined function, which is called length. Add the code given below in the script. alert ("The length of given input is:" + $ ("#Validatetextbox").val ().length);

Comments
  • What UI are you using? WinForms, WPF, or UWP or ASP.NET MVC?
  • Looks like web forms given the event.
  • Instead of setting Enabled, try changing color
  • The next step would be to set label4.text = ""; after calculate_salary.enable = true;
  • @Uyur99 Try setting the label value to: label4.Text = string.empty; or you could set the visibillity to false: label4.Visible = false;
  • The Regex("/^[A-Za-z]+$/") does match when the text box is empty so it will still display en error. You need to add an option for when the textbox is empty and than Calculate_Salary.Enabled = false but you don't display an error