Can any one give the perfect regular expression for
1. US date format (mm/dd/yyyy).
2. Alpha Numerics.
3. Alpha Numerics with spaces.
4. age betwenn 15-100.
in java script. i have this Regex.
/^[0-9a-zA-Z]+$/ (alphanumeric that doesnot allow spaces)
/^[0-9]+$/ ( for age but not getting below 100)
/^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d+$/; (US date format)
Thanks
Can any one give the perfect regular expression for
1. US date format (mm/dd/yyyy).
2. Alpha Numerics.
3. Alpha Numerics with spaces.
4. age betwenn 15-100.
in java script. i have this Regex.
/^[0-9a-zA-Z]+$/ (alphanumeric that doesnot allow spaces)
/^[0-9]+$/ ( for age but not getting below 100)
/^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d+$/; (US date format)
Thanks
Share Improve this question edited Aug 23, 2011 at 11:28 Chakradhar asked Aug 23, 2011 at 8:41 ChakradharChakradhar 7736 gold badges14 silver badges29 bronze badges 1- gskinner./RegExr I would try to write some for you but they would catch that I am that weird VIM guy. – James Andino Commented Aug 23, 2011 at 11:24
2 Answers
Reset to default 2- I'm guessing you dont want the brackets
(0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3[01])/[0-9]{4}
Note this doesn't validate that the number of days is valid in that month
[a-zA-Z0-9]+?
[a-zA-Z0-9\s]+?
- I wouldn't use regex for this but anyway
(1[5-9]|[2-9][0-9]|100)
for these you may need to add the bol an eol chars depending on where your input is ing from (as in is there other content in the line).
Tested using regexpal
I think http://regexpal./ is a very good resource to learn regexp in general. Combine this with the mozilla docs: https://developer.mozilla/en/JavaScript/Guide/Regular_Expressions
For 4, you are better of parsing the string as a number with parseInt
or parseFloat
and then paring them with an if
.