0

I tried with the following regular expression

/^([a-z0-9])+(,[a-z0-9]+)*$/

My text field string looks like the following csv format as

123,456,789,012

But the above expressions fails for me, main thing is whitespaces are not allowed with the given text field string.

ajp15243
  • 7,704
  • 1
  • 32
  • 38
Pez
  • 1,091
  • 2
  • 14
  • 34

2 Answers2

4
  1. If you want to validate textfield with value like csv format ,

    you can use : /^([a-z0-9]+(?:,[a-z0-9]+)*)$/gm

    Which will accept 123,456,789,012

    And will reject 123, 456, 789, 012 // Those containing spaces

  2. If you want to match something like this (num,num,num,num)

    you can use :

    /^(\([a-z0-9]+(?:,[a-z0-9]+)*\))$/gm

DEMO

Explanation :

enter image description here

Sujith PS
  • 4,776
  • 3
  • 34
  • 61
1

If I understand you correctly, here's the version with white-spaces allowed:

var pattern = /^[a-z0-9]+(?:, ?[a-z0-9]+)*$/;
!!pattern.exec('123,456,789,012'); // true
!!pattern.exec('123, 456, 789, 012'); // true
ikaruss
  • 491
  • 4
  • 13