Solved

phone number validation with "isNaN"

Posted on 2003-10-23
10
2,831 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

730 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