jQuery Validtion - Regex
I need to validate a field.
Validation Rule: Multiple Values can be en开发者_StackOverflowtered in text box in format of 1,2,3,4... Range (1 to 7) and are only separated by ','.
Just as something to keep for later, there is an awesome website for testing and tweaking regular expressions that a colleague forwarded to me.
I have a few answers for you, this allows no spaces ("1,2,3,4,5,6,7"):
^[1-7]$|^([1-7],)+[1-7]$
This allows arbitrary spacing between a comma and the next number ("1, 2, 3"):
^[1-7]$|^([1-7],\s*)+[1-7]$
This allows arbitrary spacing as long as it's number, comma, number etc. (" 1 , 2, 3 ,4"):
^[1-7]$|^(\s*[1-7]+\s*,\s*)+[1-7]$
I'm no expert, there are probably more concise ways to do this. That's the Regex part. For jQuery validation (assuming you haven't already) check out "bassistance.de/jquery-plugins/jquery-plugin-validation/" (sorry can't post more than one link due to reputation). Use it all the time and it's awesome and pretty easy.
精彩评论