Solved

phone number validation with "isNaN"

Posted on 2003-10-23
10
2,809 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 125 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now