...

View Full Version : Need help w/script -- a validating function



schmazz
11-12-2004, 08:42 PM
Hi all,

As you can see I'm a newbie. I've been in a basic CIS class for a few weeks now. We've been learning Javascript slowly but surely, but I still think it's a little fast for me. I've been able to complete my projects by using my books and notes, but this week I'm having troubles debugging. I can't discern the problem's with Firefox's javascript console...

With this function (and yes, I have it called correctly from my HTML, so don't worry about that ;)) I need to validate a phone number from a form. I need to ensure that the phone number has a valid area code of either 541 or 503. Also, it needs to be 10 digits long. After that is all verified, I need an alert that lets the user know that the number is valid.

Who can pinpoint my error(s)?


function validPhone (f){
var phone=f.phone.value
var prefix=phone.substr(0,3)
var long=phone.length
var numeros=parseInt(phone)

if ((prefix != "541")||(prefix!="503")){
alert("That is not a valid Oregon area code. Please re-enter");
return false;
}

if (long!=10){
alert("That isn't long enough");
return false;
}

if (isNaN (numeros)){
alert("Please enter only numbers");
return false;
}

alert("That is a valid phone number. Thanks");
return true
}


Thanks for any insight!

Kristina

hemebond
11-12-2004, 09:11 PM
Well, you parse the ints out of the string then check that they're numbers. They can't be anything but numbers after using parseInt.

Other than that I don't see anything wrong. What errors are you getting?

schmazz
11-12-2004, 09:19 PM
Well, it doesn't run at all :)

For errors, I'm getting
1. Missing variable name
var long=phone.length

2. validPhone is not defined

Thanks,
Kristina

Aha. Long is a reserved JS word... let me change that and see if it works.
Ok, it seems kind of ok. But then...
When I put in a correct phone number, I am still getting the my alert "That is not a valid Oregon area code". What is wrong with my logic?

Puffin the Erb
11-12-2004, 09:23 PM
Your first condition should use logical 'and' like so :

if((prefix != "541") && (prefix != "503"))

Just as a variation, you might want to compare your approach with a solution using regular expressions. Both approaches are equally valid.

Regular expression approach :

function validate()
{
// a variable to hold the reference to the phone number field
t = document.forms[0].telnumber;
//first check length of phone number
if(t.value.length != 10)
{
alert("The phone number is not the correct length.");
t.focus();
return false;
}
//now check if the number starts correctly
if(t.value.match(/(^541)|(^503)/)==null)
{
alert("The phone number should start with 541 or 503.");
t.focus();
return false;
}
//check if all ten characters are digits
if(t.value.match(/\d{10}/)==null)
{
alert("The phone number should contain ten digits.");
t.focus();
return false;
}
alert("The phone number," + t.value + " ,is valid.");
return true;
}
</script>

7stud
11-12-2004, 09:27 PM
Hi,

Generally, when you ask for help, you need to say what you want the script to do, and what it actually does, plus any errors you receive and what line in your code the errors correspond to(e.g. a comment on the line that says: "THIS IS WHERE THE %$##$ ERROR I'VE SPENT 4 HOURS TRYING TO FIX IS". Making the font color red is a good choice for a comment like that, plus adding a redfaced, angry smilie, :mad:, can be cathartic too.)

One problem with your function is your choice of the 'logical operator' ||. The code within an if-statement is executed when the if-statement conditional is true. So, let's look at this conditional:


if ((prefix != "541")||(prefix!="503")){
alert("That is not a valid Oregon area code. Please re-enter");
return false;
}


Let's say the user enters "541" as the area code, which makes it a valid area code. This:

prefix != "541"

evaluates as false because, in fact, the area code does equal "541", and this:

prefix!="503"

evalutes as true because the area code does not equal "503". So, your if conditional becomes this:


if (false || true){
alert("That is not a valid Oregon area code. Please re-enter");
return false;
}

For an || conditional to evaluate as true, only one term has to be true. That means the alert() in the if statement will execute. So, even though a user enters a valid area code, they will get a message saying the area code is invalid.

schmazz
11-12-2004, 09:31 PM
Puffin the Erb, you nailed it. I am really, really new to JS so I try to stick to altering examples my teacher has given me, so the long latter explanation didn't help to much. But you corrected my operator, and now it makes sense (and runs, too). I'm not very mathmatically inclined so it's taking me a while to get this kind of coding down (anything slightly math-related scares me, frankly).

And 7stud, thanks for the tips on how to better ask for help. Puffin barely beat you to the solution, though ;)

And hemebond, thanks for starting!

Thanks,
Kristina

7stud
11-12-2004, 09:37 PM
Aha. Long is a reserved JS word

What are you using for a text editor? You can buy editors for $30 online(e.g. EditPlus) and there are some that are free(e.g. crimsoneditor), which have syntax highlighting, so it is readily apparent when you are using a keyword, have missed a closing paranthesis or bracket, etc. Mine has an autocomplete feature that I have set up so when I type:

dgi

it automatically writes:

document.getElementById(" ");

which is a huge time saver. :)

7stud
11-12-2004, 09:40 PM
I try to stick to altering examples my teacher has given me, so the long latter explanation didn't help to much.ttt

:(

schmazz
11-12-2004, 09:50 PM
ttt

:(
What?

ttt? I am not familiar with your crazy shorthand jargon.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum