PDA

View Full Version : form validation


speece
05-08-2003, 12:12 AM
Hello, I'm a Javascript beginner, and I have a question about the if statement.

I'd like to do a form validation. I'd like to know if the credit card owner's name should be filled by the user who has chosen to pay by credit card.

//Check for a card owner's name
if((document.frmEnquiry.payment_method.value == "creditcard") && (document.frmEnquiry.card_owner.value == ""))
{
errorMsg += "\n\tName on card \t- Enter the card owner's name";
}

This code didn't work. I'd really appreciate it if someone can help me.

Thank you.

chrismiceli
05-08-2003, 12:16 AM
what error are you getting, did you ever declare errorMsg?

beetle
05-08-2003, 12:26 AM
is payment_method a SELECT element? If so, just looking at the value property won't work.

var f = document.frmEnquiry;
if ( f.payment_method.options[f.payment_method.selectedIndex].value == "creditcard" && f.card_owner.value == "" )
{
// code here
}
For what it's worth, I maintain a javascript form validation script that will handle all this, and even verify a credit card number against established card rules and the LUHN checksum. Click the fValidate link in my sig to check it out.

chrismiceli
05-08-2003, 12:39 AM
i knew you would do that beetle :)
Always posting about fvalidate, I even think i recommended it once!

beetle
05-08-2003, 12:43 AM
Hehe, what can I say? I didn't make it just to keep it to myself :D

I really need to get the next version done - can't work on it tonight though, going to see X-Men 2!!! :cool:

speece
05-08-2003, 04:39 AM
Thank you for you replies, Chrismiceli and Beetle.

" var errorMsg" was declared and initialised at the beginning.

I'm actually using RadioButton for the payment_method. In this case, should I replace your code
[f.payment_method.selectedIndex] to [f.payment_method.checkedIndex]? I've tried this, but it didn't work.

Thank you.

cheesebagpipe
05-08-2003, 04:43 AM
//Check for a card owner's name
if((document.frmEnquiry.payment_method[n].checked) && (document.frmEnquiry.card_owner.value == ""))

Set n to the position of the 'payment_method' radio button with the value="creditcard" (first one = 0, second = 1, etc.)

speece
05-08-2003, 05:00 AM
Thank you very much. It worked!