简体   繁体   中英

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]+$

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM