-2

I have a form where the user is asked to enter a comma separated list of numbers (integers). How do I check their input with jquery during validation to verify that they have only entered numbers and commas?

DaveW
  • 5
  • 4

3 Answers3

1

You can evaluate the input (single digits) with this Regex:

^[0-9](,[0-9])*$

For long numbers, the regex is:

^[0-9]+(,[0-9]+)*$

It forces user to avoid blanks or tabs.

To allow 0248 but not 00558, the new regex is:

^(([0]?[1-9][0-9]*)|[0])(,(([0]?[1-9][0-9]*)|[0]))*$

you can test it on regex101 here

Lithium
  • 131
  • 6
  • Thanks this is working but only allows for one digit and then a comma. I am looking for any length of number in the comma separated list eg. 777,12,345,233,3 My code is: $.validator.addMethod("mynumber", function (value, element) { return this.optional(element) || /^[0-9](,[0-9])*$/.test(value); }, "Please specify the correct number format"); – DaveW Aug 14 '15 at 12:43
  • Almost perfect however I notice I can still start a number with a 00 eg. 00558. With little knowledge of Regex I am unsure if that can be avoided? – DaveW Aug 17 '15 at 06:37
  • Are you sure? The last version disable that possibility. When using 00558 in Regex 101 it says 00558 doesn't match the pattern. Which is good: [link with test](https://regex101.com/r/yF0kE6/3) – Lithium Aug 17 '15 at 07:48
  • I beg your pardon - it does! Thank you very much for your time – DaveW Aug 17 '15 at 08:47
1

To allow any positve integer numbers in combination with blanks or tabs you could test with:

/^(\d+)(,\s*\d+\s*)*$/.test('1, 8   ,22')
--> true

Update:

If I understood your comment right and you also want to exclude numbers starting with a double '0' then the following should do the job:

var re=/^(\d?[1-9]+\d*)(,\s*\d?[1-9]+\d*\s*)*$/;
['1,2,3', 
 '01,02,03 , 040004, 05', 
 '001,002,3,4'].forEach(function(s){console.log(s,re.test(s));});

results:

"1,2,3"                 true
"01,02,03 , 040004, 05" true
"001,002,3,4"           false

Second edit

Actually, coming back to this question again I noticed that my previous solution excluded the '0' as a valid input. Here is the RegExp now that will allow that too (changed the [1-9]+-patterns to [1-9] according to Lithium's comment):

/^\s*(\d?([1-9]\d*)|0)\s*(,\s*(\d?[1-9]\d*|0)\s*)*$/

Unfortunately, it is not very short and snappy any more but it works.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • thank you this worked for me. I would like to allow numbers like 0773 but not 00265 - any ideas? – DaveW Aug 14 '15 at 13:13
  • What is the difference?!? The double '00' at the beginning or the total length of the number? – Carsten Massmann Aug 14 '15 at 15:06
  • I am trying to prevent the 00 at the beginning. I am not concerned about the length of number – DaveW Aug 17 '15 at 06:39
  • @cars the + in [1-9]+ is not useful. only one occurrence is enough then you can have multiple \d* – Lithium Aug 17 '15 at 08:30
  • @Lithium: Yes, you are absolutely right. It was something I forgot to remove after having played around with it. Thanks for pointing it out - just changed it! – Carsten Massmann Aug 17 '15 at 08:48
-1

Use the .split() method and validate every number in a loop. Or, if you only need to check if it is a number (no range or any other restrictions) then you can always use a regex (see the RegExp Object)

Community
  • 1
  • 1
litelite
  • 2,857
  • 4
  • 23
  • 33