Sreedhar
Sreedhar

Reputation: 30045

jQuery Validtion - Regex

I need to validate a field.

Validation Rule: Multiple Values can be entered in text box in format of 1,2,3,4... Range (1 to 7) and are only separated by ','.

Upvotes: 1

Views: 91

Answers (1)

Nicholas Wilson
Nicholas Wilson

Reputation: 46

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.

Upvotes: 3

Related Questions