Solved

phone number validation with "isNaN"

Posted on 2003-10-23
10
2,843 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
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

705 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