I need to develop a pattern of input numbers, must start with a digit 2 and still have 6 digits or begin with "(012)2" and 6 digits, or else as "290-53-21" I made on the number of digits and the starting number 2, and on the dash(-), but I can not do at "(012)" how to write it? My code:
function checkNumber(str){
if(!str.length)
alert("error");
var tmp = new RegExp("(2?|\([032]{3}\)?)[0-9-]{6,9}");
str = str.replace(tmp, "");
if(str != "")
alert("error");
}
This one seems to do the trick:
^(((\(012\))\d|2)\d{6}|2\d{2}(-\d{2}){2})$
It's like this:
^( x | y )$
((\\(012\\))\\d|2)\\d{6}
, this will match (012)d or a 2 with ((\\(012\\))\\d|2)
and 6 digits with \\d{6}
2\\d{2}(-\\d{2}){2}
, for a 2
followed by 2 digits \\d{2}
, and followed by two times -dd (-\\d{2}){2}
var t = /^(?:\(012\))?2(?:\d{6}|\d{2}(?:-\d{2}){2})$/
t.test(2123456); // true
t.test('212-31-23'); // true
t.test('(012)212-31-23'); // true
t.test('(012)2123123'); // true
t.test('(012)212-34-56'); // true
t.test('((012)2123456245-11-11'); // false
Here's how it works:
/^ - indicates the beggining
(?:(012))? - accepts (012) or nothing
2 - the initial 2
(?:A|B) - accepts either 6 digits like so: 'aaaaaa' or 'aa-aa-aa'
where A = \\d{6}
and B = \\d{2}(?:-\\d{2}){2}
Hopefully that helps.
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.