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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Form Validation 1

Status
Not open for further replies.

Jozshi

Programmer
Sep 20, 2001
18
GB
Hey ppl!

I have a slight problem and just cant seem to solve the problem I am having with form validation to check a radio button has been chosen.

Basically I have 4 questions with 2 radio buttons to each question, one is 'Yes' and the other is 'No' (default is No).

Now on the first 2 questions it doesnt matter what they choose, but on the last 2 they must choose 'yes' in order for the form to submit.

If they havent answered 'Yes' on either of the last 2 Radiobuttons then I want a messagebox prompt to say so.

I have acquired numerous javascript examples for this, but they all use a array count of the radio buttons, but as you can appreciate thats useless as there will always be 4 buttons checked.

Any ideas or websites to check out etc etc would be great.

Thanks in advance!

- F8i
 
show us what you have for now (i mean html code of your form & functions you use to validate)

as for me - too lazy to start helping you from a total zero :) Victor
 
Ok, here is the code!
-----------------------------------------------------------

<html>
<head>
<title>Validation Form</title>
<link rel=&quot;STYLESHEET&quot; type=&quot;text/css&quot; href=&quot;../style.css&quot;>
<script language=&quot;JavaScript&quot;>
<!--
function emptyvalidation(entered, alertbox)
{
with (entered)
{
if (value==null || value==&quot;&quot;)
{if (alertbox!=&quot;&quot;) {alert(alertbox);} return false;}
else {return true;}
}
}
function billformvalidation(enquiryForm)
{
with (enquiryForm)
{
if (emptyvalidation(dom_bankname,&quot;Please enter the name of your Bank&quot;)==false) {dom_bankname.focus(); return false;};
if (emptyvalidation(dom_accname,&quot;Please enter your Account Name&quot;)==false) {dom_accname.focus(); return false;};
if (emptyvalidation(dom_sortcode,&quot;Please enter your Sortcode&quot;)==false) {dom_sortcode.focus(); return false;};
if (emptyvalidation(dom_accnumber,&quot;Please enter your Account Number&quot;)==false) {dom_accnumber.focus(); return false;};
if (emptyvalidation(dom_badd1,&quot;Please enter your address&quot;)==false) {dom_badd1.focus(); return false;};
if (emptyvalidation(dom_btown,&quot;Please enter a town&quot;)==false) {dom_btown.focus(); return false;};
if (emptyvalidation(dom_bpcode,&quot;Please enter your postcode&quot;)==false) {dom_bpcode.focus(); return false;};
if (FUNCTIONHERE(dom_q3,&quot;You must answer yes to this question before you can continue&quot;)==false) {dom_q4.focus(); return false;};
if (FUNCTIONHERE(dom_q4,&quot;You must answer yes to this question before you can continue&quot;)==false) {dom_q3.focus(); return false;};
}
}
</head>
<body topmargin=&quot;0&quot; bottommargin=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; leftmargin=&quot;0&quot; rightmargin=&quot;0&quot;>
<form action=&quot;dom-step4.asp&quot; method=&quot;post&quot; name=&quot;firstform&quot; onsubmit=&quot;return billformvalidation(this)&quot;>
<table width=&quot;400&quot; align=&quot;center&quot;>
<tr>
<td colspan=&quot;5&quot;>
<div align=&quot;right&quot;><img src=&quot;../imagesweb/spacer.gif&quot; width=&quot;100%&quot; height=&quot;7&quot;></div>
</td>
</tr>
<tr>
<td colspan=&quot;5&quot;>
<div align=&quot;right&quot; class=&quot;titleform&quot;><b>Bank Details</b></div>
</td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot;>Bank Name: <b><font color=&quot;#FF0000&quot;>*</font></b></td>
<td colspan=3>
<input name=&quot;dom_bankname&quot; type=&quot;TEXT&quot; size=&quot;20&quot; tabindex=&quot;1&quot;>
</td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot;>Account Name: <b><font color=&quot;#FF0000&quot;>*</font></b></font></td>
<td colspan=3><input name=&quot;dom_accname&quot; size=&quot;20&quot; tabindex=&quot;2&quot;></td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot;>Sort Code: <b><font color=&quot;#FF0000&quot;>*</font></b></td>
<td colspan=3><input name=&quot;dom_sortcode&quot; type=&quot;TEXT&quot; size=&quot;35&quot; tabindex=&quot;3&quot;></td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot;>Account Number: <b><font color=&quot;#FF0000&quot;>*</font></b></td>
<td colspan=3><input name=&quot;dom_accnumber&quot; type=&quot;TEXT&quot; size=&quot;35&quot; tabindex=&quot;4&quot;></td>
</tr>
<tr>
<td colspan=5></td>
</tr>
<tr>
<td colspan=5>
<div align=&quot;right&quot; class=&quot;titleform&quot;><b>Billing Details</b></div>
</td>
</tr>
<tr>
<script language=&quot;JavaScript&quot;>
function updaterecontact()
{
if (window.document.firstform.dom_badd1.value == window.document.firstform.dom_useradd1.value ) {
window.document.firstform.dom_badd1.value='';
window.document.firstform.dom_badd2.value='';
window.document.firstform.dom_btown.value='';
window.document.firstform.dom_bcounty.value='';
window.document.firstform.dom_bpcode.value='';
}else{
window.document.firstform.dom_badd1.value=firstform.dom_useradd1.value;
window.document.firstform.dom_badd2.value=firstform.dom_useradd2.value;
window.document.firstform.dom_btown.value=firstform.dom_usertown.value;
window.document.firstform.dom_bcounty.value=firstform.dom_usercounty.value;
window.document.firstform.dom_bpcode.value=firstform.dom_userpcode.value
}
}
</script>
<td colspan=&quot;5&quot; align=&quot;center&quot;>
<input type=button onMouseup=&quot;updaterecontact()&quot; value=&quot;Use Customer Details&quot; tabindex=&quot;5&quot;>
</td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot;>Billing Address: <b><font color=&quot;#FF0000&quot;>*</font></b></td>
<td colspan=3><input name=&quot;dom_badd1&quot; type=&quot;TEXT&quot; size=&quot;35&quot; tabindex=&quot;6&quot;></td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot; height=&quot;31&quot;>Address Continued: </td>
<td colspan=3><input name=&quot;dom_badd2&quot; type=&quot;TEXT&quot; size=&quot;35&quot; tabindex=&quot;7&quot;></td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot;>Town: <b><font color=&quot;#FF0000&quot;>*</font></b></td>
<td width=&quot;80&quot;><input name=&quot;dom_btown&quot; type=&quot;TEXT&quot; size=&quot;20&quot; tabindex=&quot;8&quot;> </td>
<td width=&quot;41&quot;>County: </td>
<td align=right width=&quot;60&quot;><input name=&quot;dom_bcounty&quot; type=&quot;TEXT&quot; size=&quot;10&quot; tabindex=&quot;9&quot;></td>
</tr>
<tr>
<td width=&quot;16&quot;> </td>
<td width=&quot;109&quot;>Postal Code: <b><font color=&quot;#FF0000&quot;>*</font></b></td>
<td colspan=1 width=&quot;80&quot;><input name=&quot;dom_bpcode&quot; type=&quot;TEXT&quot; size=&quot;20&quot; tabindex=&quot;10&quot;> </td>
<td colspan=2 rowspan=2 valign=bottom> </td>
</tr>
<tr bgcolor=&quot;#CCE0F5&quot;>
<td colspan=&quot;5&quot;><img src=&quot;#&quot; height=&quot;1&quot;></td>
</tr>
<tr>
<td width=&quot;16&quot;><a href=&quot;./uohelp/step3_q1.html&quot; target=&quot;_blank&quot;><img src=&quot;../images/qmark.gif&quot; alt=&quot;&quot; border=&quot;0&quot; align=&quot;left&quot;></a></td>
<td colspan=&quot;3&quot;>Do you have any special needs for Atlantic to take into account?</td>
<td align=&quot;right&quot;><input class=checkbox type=&quot;radio&quot; name=&quot;dom_q1&quot; value=&quot;Yes&quot; tabindex=&quot;11&quot;>Yes  <input tabindex=&quot;12&quot; class=&quot;checkbox&quot; type=&quot;radio&quot; name=&quot;dom_q1&quot; value=&quot;No&quot; checked>No</td>
</tr>
<tr>
<td width=&quot;16&quot;><a href=&quot;./uohelp/step3_q2.html&quot; target=&quot;_blank&quot;><img src=&quot;../images/qmark.gif&quot; alt=&quot;&quot; border=&quot;0&quot; align=&quot;left&quot;></a></td>
<td colspan=&quot;3&quot;>Would you like to receive any further information?</td>
<td align=&quot;right&quot;><input class=checkbox type=&quot;radio&quot; name=&quot;dom_q2&quot; value=&quot;Yes&quot; tabindex=&quot;13&quot;>Yes  <input class=&quot;checkbox&quot; tabindex=&quot;14&quot; type=&quot;radio&quot; name=&quot;dom_q2&quot; value=&quot;No&quot; checked>No</td>
</tr>
<tr>
<td width=&quot;16&quot;><a href=&quot;./uohelp/step3_q3.html&quot; target=&quot;_blank&quot;><img src=&quot;../images/qmark.gif&quot; alt=&quot;&quot; border=&quot;0&quot; align=&quot;left&quot;></a></td>
<td colspan=&quot;3&quot;>Have you read the Atlantic <a href=&quot;#&quot;>Terms and Conditions?</a></td>
<td align=&quot;right&quot;><input class=checkbox type=&quot;radio&quot; name=&quot;dom_q3&quot; value=&quot;Yes&quot; tabindex=&quot;15&quot;>Yes  <input class=&quot;checkbox&quot; tabindex=&quot;16&quot; type=&quot;radio&quot; name=&quot;dom_q3&quot; value=&quot;No&quot; checked>No</td>
</tr>
<tr>
<td width=&quot;16&quot;><a href=&quot;./uohelp/step3_q4.html&quot; target=&quot;_blank&quot;><img src=&quot;../images/qmark.gif&quot; alt=&quot;&quot; border=&quot;0&quot; align=&quot;left&quot;></a></td>
<td colspan=&quot;3&quot;>Do you understand that you have completed a legally binding contract?</td>
<td align=&quot;right&quot;><input class=checkbox type=&quot;radio&quot; name=&quot;dom_q4&quot; value=&quot;Yes&quot; tabindex=&quot;17&quot;>Yes  <input class=&quot;checkbox&quot; tabindex=&quot;18&quot; type=&quot;radio&quot; name=&quot;dom_q4&quot; value=&quot;No&quot; checked>No</td>
</tr>
<tr>
<td colspan=5 align=right><br><input class=&quot;button&quot; border=&quot;0&quot; type=IMAGE onMouseOut=&quot;MM_swapImgRestore(),this.style.cursor='default'&quot; onMouseOver=&quot;MM_swapImage('SUBMIT','','../images/nextstep_on.gif',1),this.style.cursor='hand'&quot; src=&quot;../images/nextstep_off.gif&quot; name=&quot;SUBMIT&quot; tabindex=&quot;15&quot;></td>
<tr>
</table>
</form>

-----------------------------------------------------------

Sorry, didnt remove the image links...but at least you get the idea.

Now it hasnt got the code for the Radiobutton check included because...well I dont have any as i cant seem to solve it, but everything else should be there. No code comments included either....sorry, thats just the way I wrote this particular site....in a rush!

Hope that helps...if you would rather me send a file then let me know!


Thanks!

- F8i
 
wooo.. well, i meant a small piece of code.. :(

anyway, here is one of the ways to do that (if you'd use your function)
Code:
function emptyvalidation(entered, alertbox){
if (entered.type==&quot;text&quot;){
with (entered){
if (value==null || value==&quot;&quot;) {if (alertbox!=&quot;&quot;) {alert(alertbox);} return false;}
else {return true;}
}}
else if(entered.length){
for (ii=0; ii<entered.length; ii++){
with (entered[ii]){
if (value==&quot;Yes&quot; && checked==false){
if (alertbox!=&quot;&quot;){alert(alertbox);}
 return false;
}
}}
}
return true
}

then call it just as in your other cases
Victor
 
Thanks very much.

You got my vote!

- F8i - F8i
Q:&quot;Ever notice something? Unix comes with compilers. NT comes with Solitaire.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top