Forum Moderators: open
I was hoping somebody can help me out. I am looking for a Javascript validation code that would make fields mandatory before submission. There are several good ones out there.
My problem is that I need one that would make one particular field required only if a checkbox is checked.
I have all my fields and which one I want required. However there is one field that I have that isn't required this would be the "Claims" field.
However I would like it that when this "Claims" field in checked that the textbox field next to it would automatically be required so that they can enter a dollar value on the claim.
I don't want them to select claim without giving me a dollar value.
Does anybody have or know where I can find a Javascript code like this. I can't seem to find one anywhere.
Thanks in advance!
e.g. pseudocode example
if (checkboxobkect.checked) {
claimobject.isvalid();
}
Hope this script helps you.
<HTML>
<BODY>
<form name="form1" action="1.php">
Claim: <input type=checkbox name="chkclaim">
Enter dollar value <input type=text name=dollarval>
<input type=button value="Submit" onclick="claimCheck()">
</form>
</BODY>
</HTML>
<script>
function claimCheck() {
var chkclaimVal=document.forms[0].chkclaim.checked;
if(chkclaimVal) {
var dollarvalue=document.getElementById("dollarval").value;
if(dollarvalue.length <0 ¦¦ dollarvalue=="undefined" ¦¦ dollarvalue=="") {
alert("Enter Dollar Value");
return false;
}
}
document.forms[0].submit();
return true;
}
</script>
Thanks
Prasanth J.V.R.S