Solved

phone number validation with "isNaN"

Posted on 2003-10-23
10
2,805 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now