Reputation: 133
This is my JavaScript for Mobile Number validation .
But, when I enter Valid number (without any space or dash) with 10 digit, it shows me error alert. for ex:. 4431220015
Did I miss something! Can anyone please help me or point me in the right direction!
Thanks in advance :)
JavaScript:
var phone = document.getElementById("Telefonnummer");
var RE = /^[\d\.\-]+$/;
var span = document.createElement("span");
span.innerHTML = "invalid mobile number";
span.className = "mobilenummer";
if (!RE.test(phone.value)) {
swal({
title: "error!",
content: span,
icon: "error",
});
return false;
}
if (phone.length != 10) {
swal({
title: "error!",
content: span,
icon: "error",
});
return false;
}
HTML:
<input name="Telefonnummer" id="Telefonnummer" type="tel">
Upvotes: 0
Views: 320
Reputation: 1292
Use this kind of regular expression :
^44[0-9]{8}$
Only mobile numbers which starts with 09 and with 10 digits are allowed.
And also you could create your own regular expression with this site : regex101.com
Upvotes: 0
Reputation: 3082
I think your error is simply that you use phone.length
, but phone
is the DOM element.
You want to check phone.value.length
.
Upvotes: 1