Reputation: 378
I want to make a regex that tests for mobile nos (10 digit, starting digit can be 7,8 or 9)
I worked out this -> /^(7|8|9)[\d]{9,9}$/
My code is
function testPhone()
{
var pattern = /^(7|8|9)[\d]{9,9}$/;
var phoneNo = document.getElementById('phoneNo');
if (!pattern.test(phoneNo))
{
alert("It is not valid mobile number!");
}
}
<input type="text" name="phoneNo" maxlength="10" size="10" >
<input type="submit" value="Search" onclick="testPhone()">
Somehow it always displays the alert message. I also tested my regex here (http://www.regextester.com/) . It works here. Please help.
Upvotes: 0
Views: 435
Reputation: 32807
It should be
var phoneNo = document.getElementById('phoneNo').value;
^
and regex should be
/^(7|8|9)\d{9}$/
no need of {9,9}
and [\d]
Upvotes: 1
Reputation: 263743
you forgot .value
,
var phoneNo = document.getElementById('phoneNo').value;
try this pattern,
^[7-9](\d){9}$
Upvotes: 3
Reputation: 22508
It's
var phoneNo = document.getElementById('phoneNo').value;
Note the .value
.
Oh, and your input doesn't have an id
.
Upvotes: 4