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.

5
  • 1
    stackoverflow.com/questions/6432408/… Commented Jan 24, 2014 at 5:41
  • Why are you using those parentheses? Commented Jan 24, 2014 at 5:43
  • 1
    @Pez what are your valid and invalid inputs ? Commented Jan 24, 2014 at 5:52
  • @ Sujith PS (123, 234, 345) be a invalid pattern and {123,345,567} be a valid one Commented Jan 24, 2014 at 8:03
  • @Pez So you dont want to allow whitespace , right . what about ( ) and { } ?? Commented Jan 24, 2014 at 9:51

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.