Rahul Unnikrishnan
Rahul Unnikrishnan

Reputation: 69

JavaScript regular expression for the validation of comma separated string

I want to validate whether the user enter data in this form B101,202,BBB,B902,Z-102 That is each value can be only of alphabets, digits or alphanumeric + '-' Tried this expression but it failed for alphabets and digits only condition. ^[A-Za-z0-9]+(,[A-Za-z0-9]+)*[A-Za-z0-9]+$

Upvotes: 2

Views: 2611

Answers (2)

macuto
macuto

Reputation: 1

This regex not woking with list of 1 digit numbers like 64,5,8,9

Upvotes: -1

Partha
Partha

Reputation: 402

Please try the following regex

^[A-Z0-9]+((,|-)[A-Z0-9]+)*[A-Z0-9]+$

This checks the following:

  • Starts with Alphabets or Numeric
  • Ends with Alphabets or Numeric

You can try it online at Regexr.com

Upvotes: 3

Related Questions