Sunday, May 1, 2011

asp.net validations

I want to add a validation control to check if the first name and last name of a person entered consists of characters from A-Z and not any special characters and numbers? How to do it?

From stackoverflow
  • You could use a Regular Expression validator or a Custom Validator.

  • use a regular expression validator and put the [a-zA-Z] as the reg ex to validate.
    If you want support for hyphen and underscore then put [a-zA-Z_\-]

    <asp:RegularExpressionValidator id="revalApha" runat="server" 
    ControlToValidate="TextBox1" ValidationExpression="[a-zA-Z]+$" 
    ErrorMessage="Only Alphabets allowed"> </asp:RegularExpressionValidator>
    

    I will advice against the custom validator since you need to write a function both in javascript and in c#

  • Here is a good example of using a regular expression validator for that. But don't forget O'Dell and Van Damme. :)

0 comments:

Post a Comment