Click to See Complete Forum and Search --> : Validation script


Aronya1
08-16-2003, 09:44 PM
All hail the JS Kings!

Looking for help on something that should be fairly simple for someone who knows what they are doing, and I'm assuming this is a javascript question. Please correct me if I'm wrong.

I'm working on a form that will have various fields to be validated, but I also want to force the user to select a radio button before the Submit button will work (like agreeing to a software package's terms of use before it will install).

I can handle the form validation for the rest of the fields, but I'd like to have the radio button selection checked first. No point in validating the rest if they don't agree to the terms, right?

Also, I'd like to pop up a message window if they click Submit without selecting the proper radio button; "You must accept the terms of this agreement..." This part isn't crucial, but it would be nice to let the user know what they did wrong.

Anybody care to take a crack at this? Or point me to a good site for something like this?

Many thanks.
Aronya1

Khalid Ali
08-17-2003, 01:38 AM
what you can do is in the fubmit event of the form add a condition like this

<form id="form1" action="" onsubmit="">
<p>
<input type="text" name="t1"/>
<input type="radio" id="rg_1"/>Please select
<input type="button" value="process" onclick="if(document.getElementById('rg_1').checked){Process()}else{alert('please select radio')}"/>
</p>
</form>

in the above example if the radio button is selected then Process function is called which takes care of rest of the validation else user gets a alert message..

Aronya1
08-17-2003, 02:17 AM
Hi Kahlid,

Thanks for the response. I've tried what you gave me & it works partially. What happens is that the form calls my PHP script, regardless of what is checked or not.

If the box is checked, it calls my PHP script. This is good.
If the box is NOT checked, it pops up the message, then calls the script anyway.

Radio buttons have to have the same ID, otherwise you can select more than one at a time. I thought that might be the problem, so I changed the radio buttons to a checkbox. Same thing happens.

Any other ideas?

Khalid Ali
08-17-2003, 08:09 AM
yeah u r right just add return false after the alert statement..here take a look

<input type="submit" value="process" onclick="if(document.getElementById('rg_1').checked){Process()}else{alert('please select radio');return false;}"/>

Aronya1
08-17-2003, 12:44 PM
Hi Kahlid,

Thanks for your help. I still had to struggle with it + get my wife to help. Had to sandwich the form validation between your code to make it work properly. If you're interested, here's the result:

<input name="Submit" type="submit" id="Submit" onClick="if(document.getElementById('authorize').checked) {MM_validateForm('Company Name','','R','Company City','','R','Company State','','R','Company ZIPCODE','','R','Company Email','','NisEmail','Officer Name','','R','Officer Title','','R','Officer Phone','','RisNum','Officer Email','','NisEmail','Company Street','','R');return document.MM_returnValue} else{alert('please select radio');return false;}" value="Submit Form">


Thanks again.
Aronya1

Khalid Ali
08-17-2003, 12:50 PM
You are welcome..
The code I posted is a generic logic with example code to validate form fields,as you have ameded it to your needs thats all was the purpose.Good work.