-4

I need to validate an input field, which should contains blocks separated by commas,and maximum is 50 blocks, each block must be 8 characters long, only number and letter be allowed.

Examples: 1F223142,23FH2324,3232UD23

I searched but I cannot find a matching one, so how should my regex be ?

jimbo R
  • 227
  • 1
  • 3
  • 14
  • The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Dec 29 '18 at 06:56
  • `([A-Z0-9]{8})(,\1){0,49}`- https://regex101.com/r/ALU4MI/1 – splash58 Dec 29 '18 at 10:56

1 Answers1

0

Try this one.

/([A-Z0-9]{8},){0,49}([A-Z0-9]{8}){1}/g

This will look for (8 capital letters or numbers followed by a comma) for minimum 0 to maximum 49 times. Then it will look for (8 capital letters or numbers followed by a comma) one time.

In this way a user can enter a Single block NOT followed by comma or maximum 50 blocks seperated by commas with the last one NOT followed by a comma.

You will need to compare the lengths of the original input and the result from input. For Example:

let a = "1F223142,23FH2324,3232UD23";
let r = /([A-Z0-9]{8},){0,49}([A-Z0-9]{8}){1}/g.exec(a)[0].length;
if (a.length == r.length) {
   //valid input
} else {
   //invalid input
}
Iqbal
  • 151
  • 8