Forum Moderators: open

Message Too Old, No Replies

Required field when checkbox is selected

         

Makaveli

9:25 pm on Mar 7, 2006 (gmt 0)

10+ Year Member



Hello,

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!

Dijkgraaf

5:34 am on Mar 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well without you giving us specific code examples it is a bit hard to give you exact code.
But esentially you will need to put the mandotory field validation code for that field within and if statement that checks to see if the checkbox is checked.

e.g. pseudocode example
if (checkboxobkect.checked) {
claimobject.isvalid();
}

prasanth jvrs

6:34 am on Mar 8, 2006 (gmt 0)

10+ Year Member



Hi,

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

Makaveli

9:48 pm on Mar 8, 2006 (gmt 0)

10+ Year Member



Thanks prasanth,

The script didn't work on the first try but after playing around with it, it's working now.

I greatly appreciate your help!