Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone tell me what's wrong w

Status
Not open for further replies.

WannaLearn

Programmer
Jul 10, 2001
210
US
Can anyone tell me what's wrong with this code? It accepts the input of '5ee56' why? How can I also make sure that they entered 10 digists as well?
This is what I have so far:

var telephone = document.form.phone.value
var telephone = parseInt(telephone, 10)
if(isNaN(telephone)) {
alert("Please enter The Telephone Number")
document.form.phone.value=""
document.form.phone.focus();
return false;
}
 
hi

may be this would work for you?

var telephone = document.form.phone.value
if(!telephone.match(/\d{10}/)){
alert("Please enter The Valid Telephone Number")
document.form.phone.value=""
document.form.phone.focus();
document.form.phone.select();
return false;
}

/\d{10}/ - regular expression, d - digit 10 - that there'have to be 10 digits Victor
 
Hi that worked great. Can you tell me what this does exactly: (!telephone.match(/\d{10}/))
 
Hi vituz, one last question, if I wanted the number to put in a space or a dash (-) between the areacode, first 3 digits and last 4 digits, how do I do that?

Like this: someone enters 6305555555, it changes it to: 630 555 5555 or 630-555-5555? Is that doable?
 
Hi WannaLearn,

try this,

if(telephone.match(/\d{10}/)) {
document.form.phone.value = "" + telephone.substring(0,3) + "-"
+ telephone.substring(3,6) + "-"
+ telephone.substring(6,10)
}
else if(!telephone.match(/\d{3}(\s|-)\d{3}(\s|-)\d{4}/)) {
alert("Please enter The Valid Telephone Number")
document.form.phone.value=""
document.form.phone.focus();
document.form.phone.select();
return false;
}

for more information about regular expression, you can visit this site.


Hope this helps, Chiu Chan
WebMaster & Software Engineer
emagine solutions, inc
cchan@emagine-solutions.com
 
Hi PepperPepsi, thank-you so much, it helped me out.
Can you please tell me what: '(/\d{3}(\s|-)\d{3}(\s|-)\d{4}/))' means? As well as: '(/\d{10}/))' means? I'd appreciate that much more.
Thanks a lot.
 
I'll explain to you WannaLearn,


(/\d{3}(\s|-)\d{3}(\s|-)\d{4}/) is a regular expression. It is searching for this particular pattern:
\d{3} = 3 digits
(\s|-) = a space or a dash....| means or
\d{3} = 3 digits again
(\s|-) = a space or a dash....| means or...again
\d{4} = four more digits...


\d{10} = matches for 10 digits in a row.

it fits like this 547-236-4785 or 547 236 4785 ...those number are replaceable with any numbers. I just wanted to throw some in for you.

When you pasted in the code, it ended like )). One of those parenthesis is closing the if condition.

This is all part of the RegExp object. Take a look on msdn.microsoft.com...find the javascript scripting pages. They changed the site around so you may have to do some searching.

Mike
 
WannaLearn,

Ok the dashes were replaced by the smilies...dumb smilies getting in the way.

Mike
 
Hi,

Hope this will give you some idea,

/d{3} - A 3 "d"igit number
/d{4} - A 4 "d"igit number
\s - represents the whitespace character
| - OR operator
(\s|-) - means take the whitespace OR character "-"

thanks Chiu Chan
WebMaster & Software Engineer
emagine solutions, inc
cchan@emagine-solutions.com
 
PepperPepsi, i've been reading up on what you suggested (the website) and I have another question: : for the term: '(/\d{3}(\s|-)\d{3}(\s|-)\d{4}/))' what does the blue-ones specify? I mean, the ones in green pesicfy that it is 3 digits followed by a space/character, then another 3 digits and another space/character and then finally 4 digits, so why is there another / in from of \d?
 
Hi WannaLearn,

The two forward slashes indicates where the regular expression begin and end. You can also do

var MyRegEx = new RegExp("\d{10}");

It is equivalent to var MyRegEx = /\d{10}/;

Hope this helps,


Chiu Chan
WebMaster & Software Engineer
emagine solutions, inc
cchan@emagine-solutions.com
 
Pepperpepsi, I have a similar question that WannaLearn has asked...is it possiable to have the phone field add the dashes or spaces open leaving that filed and going to another field (via TAB inserts or just selecting the next field)?? So far what I have is that there are like 6 questions, first 3 are text and the rest are drop-down choices, the names field comes first and then phone and then the drop down choices, I would like the phone field to have the dashes and spaces inserted upon leaving that field. I have the entire form as onSubmit, I tried making the JS for the phonenumber seperate and then calling that as onKeyUp or onKeyDown, but that makes the user enter the phone field first, but I need the names entered first, then phonenumber then the option choices...
Am I explaining it right?? I have not failed; I merely found 100,000 different ways of not succeding...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top