I need a regular expression in JavaScript
to validate an input like this:
5461500,3256990012,8122715,3148419530,[...]
- Must validate only seven or ten digit numbers
- Ten-digit numbers must always start with 3
- There should be no letters, spaces, or characters other than commas that separate the values
- It does not matter if there are numbers with seven or ten consecutive digits or if there are only one of these lengths
I am using this expression, but it is insufficient:
/^(\d+\,?)+$/
Effectively, it restricts everything that is not a number or a comma (,). But I still need, if they are numbers, to be seven or ten digits long.
You could validate what you are looking for using the following regular expression:
Explanation:
3\d{9}
\d{7}
|
, that is,(3\d{9}|\d{7})
(,(3\d{9}|\d{7}))*
Demo
A possible solution could be:
Here more detailed:
The second group consists of 7-digit numbers or 10-digit numbers starting with 3.
The third group is made up of a comma followed by a digit (which does not consume characters because positive lookahead is being used ) or by a line ending
All this is contained in a group that can be repeated one or more times.
Here you can check different patterns with this expression.
Hello this expression meets the criteria you request:
Note the sections:
(3\d{9})
(\d{7})
,...
UPDATE: You can remove some parentheses depending on what you don't need to capture for reference.