PDA

View Full Version : Form validation woes


hyefive
10-30-2002, 02:17 AM
Hello codingforums,

I'm trying to use the following script to intercept a PROP_TYPE = "RES" form submission that doesn't include a PROP_SUB_T value. Can't figure out why I'm not getting the alert box when testing it...

<head>
<script language="javascript">

function checkForms(){
if (document.form1.PROP_TYPE.value == "RES" && document.form1.PROP_SUB_T.value == ""){
alert("Please choose a Property Type")
return false;
}
}

</script>
</head>
<body>

<table>
<form action="search.asp" method="post" onsubmit="return checkForms()" id="form1" name="form1">
<tr>
<td><input type="RADIO" name="PROP_TYPE" value="RES" onClick="opens_only_res_on();">Residential
<br><input type="RADIO" name="PROP_TYPE" value="INC" onClick="opens_only_inc_on();">Multi-Residential Income</td></tr>

<% If PROP_TYPE = "RES" Then %>
<tr>
<td><input type="checkbox" name="PROP_SUB_T" value="SFR">SFR&nbsp;<input type="checkbox" name="PROP_SUB_T" value="CND">COND</td></tr>
<% End If %>

<tr>
<td><input type="SUBMIT" value="Search"></td></tr>
</table>
</form>

</body>


I tried it without the onClick handler, and it made no difference but I've left it in anyways to give a clearer picture...


Thank you for any help you can provide!

glenngv
10-30-2002, 02:51 AM
function checkForms(f){

if (
(!f.PROP_TYPE[0].checked && !f.PROP_TYPE[1].checked)||
(f.PROP_SUB_T && !f.PROP_SUB_T[0].checked && !f.PROP_SUB_T[1].checked)
){
alert("Please choose a Property Type or Sub-Type.")
return false;
}
}
...
<form action="search.asp" method="post" onsubmit="return checkForms(this)" id="form1" name="form1">


since PROP_SUB_T may or may not appear on the page, you should checked it first for its existence before validating if any of them is checked.

hyefive
10-30-2002, 03:55 AM
Yes, you've got it!

Thank you very much
:)