Link to home
Start Free TrialLog in
Avatar of johnywhite
johnywhite

asked on

.NET Validation

I havve a form with a Regular Expression Validator.  I am trying to get it to not allow any speical characters only letters and numbers and I also want it to not allow it to start with a number.  What expression would I use and could someone also explain what the expression is doing?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Regular expression:

^[A-Za-z0-9]+$

Starting at the beginning of the string, replicate any number of upper-case letters, lower-case letters and numbers until the end of the string is reached.

Bob
ASKER CERTIFIED SOLUTION
Avatar of Ravi Singh
Ravi Singh
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of johnywhite
johnywhite

ASKER

That's what I was looking for thanks.
Regular Expression to allow alpha a first character and alpha numeric after.

     ^[a-zA-Z][a-zA-Z0-9]*$

Where:
    ^ Marks the begining of the string
    [a-zA-Z] Which only allows one alpha to start the string
    [a-zA-Z0-9] Which only allows one alpha numeric starting at the second character
    * Quantifier Which states allow zero or more of the previous character
    $ Marks the end of the string

Fernando
add the $ (end of string) symbol if you dont want leading spaces or spaces within the entire string...

^[A-Za-z]+([A-Za-z]|[0-9])*$

Actually the $ lets the regular expression engine know that there should be no more characters. If you do not place it there then you could have invalid characters at the end and as long as the string starts with an alpha followed by 1 or more alpha numeric it will validate.
thanks Fernando... I tested it with some leading spaces and mistakenly wrote that... your explanation covers what I was trying to say!
Good; glad we are on the same track. You have a great day. :=)