...

View Full Version : Disable form if checkbox is ticked



DR.Wong
05-05-2008, 10:06 PM
Hello there everyone.

I'm into PHP and Delphi dev so Javascript is a little over my head.

Can anyone help me out with a simple little script?

I have a form with two text fields and a checkbox

|=======| - Textfield 1

|=======| [-] - Textfield 2 and Checkbox 1

If there is a value in Textfield 1, Checkbox 1 must be enabled. If there is no value, then it must be disabled.

If there is a value in Textfield 1 and Checkbox 1 is checked then Textfield 2 must gain the value of Textfield 1 and both of the textfields must become disabled.

If the box is again unchecked, the textfields must once again become enabled.

Can anyone point me in the right direction? I am making a simple POS system for my business and this is for creation of an account. The checkbox would denote that the account name and the account holder are the same. [just so you know what it's going to be used for]

I have very limited JS abilities so your help would be AWESOME!!

Thanks a lot!!

-Dr. Wong

binaryWeapon
05-06-2008, 04:31 AM
<form name="yourform">
<input type="text" name="field1" onkeyup="checkCheck();">
<br><input type="text" name="field2"><input type="checkbox" name="check" onclick="toggle()">
</form>
<script>
var thingchecked=false;
function toggle()
{
if(document.yourform.check.checked==true)
{
document.yourform.field2.value=document.yourform.field1.value;
document.yourform.field1.disabled='disabled';
document.yourform.field2.disabled='disabled';
}
else
{
document.yourform.field1.disabled='';
document.yourform.field2.disabled='';
}
}
toggle();
document.yourform.check.checked=false;

function checkCheck()
{
if (document.yourform.field1.value=="")
{
document.yourform.check.disabled='disabled';
}
else
{
document.yourform.check.disabled='';
}
}
checkCheck()
</script>

You can edit your form and input names as you wish, just make sure you edit them in the code too.

Hope this helps!
~binaryWeapon



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum