The regex is formatted properly, it looks like; you want to make sure that the whole string is nothing but 10 digits.
You are trying to compare the value of an element that does not exist. You don't have an element named "text", you have a text input named "inputnumber". So:
Code:
function checknumber() {
var tendigitnum = document.getElementById("inputnumber").value;
var check=/^\d{10}$/;
if (check.test(tendigitnum)) {
window.alert("You entered"+tendigitnum);
}
else {
window.alert("Please enter a valid 10 digit number");
}
Always be on the look out for JS error messages.. they can tell you much about what the issue is.
Also, you can remove "window." from before alert; you don't need it.
Everything looks correct, except for the "window.alert" parts. Change those to just "alert". If that doesn't fix it, let me know. I'll take a closer look, in the meantime.
I just noticed - you are not doing anything to prevent the form from being submit. Set your onsubmit to "return checknumber();", then:
Code:
alert("You entered"+tendigitnum);
return true;
.. and ..
Code:
alert("Please enter a valid 10 digit number");
return false;
Bookmarks