Topic: Form with one of two radio buttons required (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29740" title="Pages that link to Topic: Form with one of two radio buttons required (Page 1 of 1)" rel="nofollow" >Topic: Form with one of two radio buttons required <span class="small">(Page 1 of 1)</span>\

 
Nimraw
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Sep 2000

posted posted 12-02-2007 10:49

Hi

My my.. this is what it looks like in this subforum.. Haven't been here for years.

Anyhow, I'm working on an order form sent through JMail, and I'm stuck on validating radio buttons.

All fields in the form are supposed to be mandatory, and I have been able to get the Javascript to validate all fields apart from the radio buttons.

Basicly I have 2 unchecked buttons (different payment methods) in the same array in the form

code:
<input type="radio" name="payment" Value="PF" >PF
<input type="radio" name="payment" Value="BG">BG




The script I'm using to validate the rest looks like:

code:
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function verify() {
var themessage = "Please fill in the following fields ";

if (document.form.firstname.value=="") {
themessage = themessage + " - Förnamn";
}
if (document.form.lastname.value=="") {
themessage = themessage + " -  Efternamn";
}
if (document.form.adress.value=="") {
themessage = themessage + " -  Adress";
}
if (document.form.zip.value=="") {
themessage = themessage + " -  Postnummer";
}
if (document.form.city.value=="") {
themessage = themessage + " -  Ort";
}

if (document.form.email.value=="") {
themessage = themessage + " -  E-mail";
}
if (document.form.phone.value=="") {
themessage = themessage + " -  Telefonnummer";
}
if (document.form.approve.checked==false) {
themessage = themessage + " -  Godkännande";
}

//alert if fields are empty and cancel form submit
if (themessage == "Please fill in the following fields ") {
document.form.submit();
}
else {
alert(themessage);
return false;
   }
}
//  End -->
</script>




but I don't know how to check that one of the two radio buttons is checked. I could go for the easy solution and have one pre-checked, but I'd prefer to have them both unchecked.

I did try adding something like:

code:
}
if (document.form.payment.checked==false) {
themessage = themessage + " -  Betalning";
}



which naturally didn't work as one of the buttons will false at all times.

The problem is that I can't really write javascript from scratch, but just manipulate and tweak scripts I find..

Is there an easy solution to this? I have googled quite a but but I guess I'm asking the wrong questions cause the results I get is not really helping (most are too complex or dealing with the vaules of the buttons rather than the true/false problem)

Thanks

zavaboy
Paranoid (IV) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 12-02-2007 11:38

I haven't coded much JS for ages, but this should be what your looking for:

code:
if (document.form.payment[0].checked==false && document.form.payment[1].checked==false) {
themessage = themessage + " -  Betalning";
}


Also, make sure you have a second line of validation that is server side. Think about what can happen if someone has JS disabled.

Nimraw
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Sep 2000

posted posted 12-03-2007 09:02

Thanks!

That works like a charm, and I can even understand why the code has to be that way

Server Side Validation you say..
I know I should, but until I figure that one out, I'm afraid the non-JS-people will end up at a non-validating page

(Edited by Nimraw on 12-03-2007 09:11)

janet1
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jan 2008

posted posted 01-20-2008 11:54

zavaboy,
thanks, your code works for my similar problem

software reviews editor



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu