QuadMedic
asked on
phone number validation with "isNaN"
I wrote this script to to validate that: 7 numbers are used and not letters,.......it works except for the letter "e". I can write this script diffrently but I want to know why this happens ?
<script language="JavaScript">
var telephone
</script>
</head>
<body>
<script language="JavaScript">
telephone = prompt("Please enter your Telephone number:","");
if (isNaN(telephone)){
document.writeln("You must enter numbers only");
} else if (telephone.length!=7){
document.writeln("Your telephone number is invalid");
} else{
document.writeln("Your telephone number is " + telephone);
}
</script>
</body>
it works with all charecters except "e"
<script language="JavaScript">
var telephone
</script>
</head>
<body>
<script language="JavaScript">
telephone = prompt("Please enter your Telephone number:","");
if (isNaN(telephone)){
document.writeln("You must enter numbers only");
} else if (telephone.length!=7){
document.writeln("Your telephone number is invalid");
} else{
document.writeln("Your telephone number is " + telephone);
}
</script>
</body>
it works with all charecters except "e"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this works for me
<body>
<script language="JavaScript">
telephone = prompt("Please enter your Telephone number:","");
if (isNaN(telephone)){
document.writeln("You must enter numbers only");
} else if (telephone.length!=7){
document.writeln("Your telephone number is invalid");
} else{
document.writeln("Your telephone number is " + telephone);
}
</script>
</body>
<body>
<script language="JavaScript">
telephone = prompt("Please enter your Telephone number:","");
if (isNaN(telephone)){
document.writeln("You must enter numbers only");
} else if (telephone.length!=7){
document.writeln("Your telephone number is invalid");
} else{
document.writeln("Your telephone number is " + telephone);
}
</script>
</body>
notice i did not define telephone globally
Did you test whit this string: 12345e7
sorry
here
<script language="JavaScript">
var telephone = prompt("Please enter your Telephone number:","");
var objRegExp = /^\d{7}$/;
if (!objRegExp.test(telephone )){
document.writeln("You must enter numbers only");
} else if (telephone.length!=7){
document.writeln("Your telephone number is invalid");
} else{
document.writeln("Your telephone number is " + telephone);
}
</script>
here
<script language="JavaScript">
var telephone = prompt("Please enter your Telephone number:","");
var objRegExp = /^\d{7}$/;
if (!objRegExp.test(telephone
document.writeln("You must enter numbers only");
} else if (telephone.length!=7){
document.writeln("Your telephone number is invalid");
} else{
document.writeln("Your telephone number is " + telephone);
}
</script>
So when you enter 123, the you get response: "You must enter numbers only"???
ASKER
sorry jaysolomon but your idea doesn't work.........thx NetGroove
Thanks for the points.
y did mine not work? phone is 7 digits if you do not need area code or hyphens?
'e' produces the appropriate message -- numbers only.
If you would like to see how you can allow letters in a phone number see the "Masking Phone Numbers" script/tutorial at http://members.aol.com/grassblad
Vinny