?
Solved

phone number validation with "isNaN"

Posted on 2003-10-23
10
Medium Priority
?
2,871 Views
Last Modified: 2008-02-26
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"
0
Comment
Question by:QuadMedic
10 Comments
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9607160
Hi,

  '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
0
 
LVL 10

Accepted Solution

by:
NetGroove earned 500 total points
ID: 9607166
The char e is allowed for exponetial number expressions.

How about this:

<script language="JavaScript">
var telephone
</script>
</head>

<body>
<script language="JavaScript">
telephone = prompt("Please enter your Telephone number:","");
if (telephone.match(/([^0-9]+)/)){
  document.writeln("You must enter numbers only.<br>This char is not allowed: <font color=red><b>"+RegExp.$1+"</b></font>");
} else if (telephone.length!=7){
  document.writeln("Your telephone number is invalid");
} else{
document.writeln("Your telephone number is " + telephone);
}
</script>
</body>

0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9607173
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>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:jaysolomon
ID: 9607182
notice i did not define telephone globally
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9607360
Did you test whit this string: 12345e7

0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9607423
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>
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9607483
So when you enter 123, the you get response: "You must enter numbers only"???
0
 

Author Comment

by:QuadMedic
ID: 9607515
sorry jaysolomon but your idea doesn't work.........thx NetGroove
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9607598
Thanks for the points.
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9611957
y did mine not work? phone is 7 digits if you do not need area code or hyphens?

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question