I want to use regular expression in JavaScript which limit number of phone in ten digit and the two first digit must begin with 06
or 07
.
0688342134
, 0721654709
;0588342134
, 0421654709
.var phoneno = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
if (inputtxt.value.match(phoneno)) {
return true;
} else {
alert("valid phone number must begin with 06 or 07 and have ten digit");
return false;
}
It doesn't begin with 06
or 07
.
Any ideas?
function checkPhone(phone) { return (new RegExp(/^0[6-7][\d]{8}$/)).test(phone); } let phones = [ '0678901234', // true: starting with '06' and 10-chars length '067890123', // false: starting with '06' but 9-chars length '06789012345', // false: starting with '06' but 11-chars length '0789012345', // true: starting with '07' and 10-chars length '0890123456' // false: 10-chars length but starting with '08' ]; phones.forEach(phone => { console.log(checkPhone(phone)); });
you can use
<input type="tel" pattern="06|07\d{8}">
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel
this is what you want:
reg = /^0[67]\d{8}$/
if(inputtxt.value.match(phoneno) && (inputtxt.value.substring(0,2)=='06' || inputtxt.value.substring(0,2)=='07')) { return true; }
Update your regular expression to:
var phoneno = /^\+?(0[67])\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
The part that matches the first two numbers changed from ([0-9]{2})
, which matches 2 of any number, to (0[67])
, which matches a 0 followed by a 6 or 7.
You can use this:
var phoneno = /^\+?(0[67])\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/
Change
[0-9]{2}
To
0[67]
Explanation
0
matches the digit zero.
[67]
matches one occurrence of either 6 or 7.
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.