...

View Full Version : Disabling 'submit'



NeoPuma
10-22-2006, 01:15 PM
Hi there,
I'm trying to create a simple check and act sorta thing. I want it, so that if 'user' and/or 'email' is blank, then disable the submit button, else enable it. I got the following code, but its failing to do what I want:


<script type="text/javascript" language="JavaScript">
function required() {
var check
if(document.updateaccount.user.value == "") {
check = 1;
}
if(document.updateaccount.email.text == "") {
check = 1;
}
if(check == 1) {
document.updateaccount.Submit.disabled = "true";
} else {
document.updateaccount.Submit.disabled = "false";
}
}
</script>

Can anyone help me please?

Thanks
NP

BarrMan
10-22-2006, 01:21 PM
What does it fail to do? Perform the validation or validate the form on time?
Can you send the form code?

NeoPuma
10-22-2006, 01:33 PM
This is the form. Check remains undefined which is good because the fields are not blank. The submit button, still remains disabled though.

Heres the form (please note it does include some php)

<form action="" method="post" name="updateaccount" id="updateaccount">
<div align="left">
<p>Username: <strong><font color="#FF0000">*</font></strong><br>
<input name="user" type="text" id="user" value="<?=$_SESSION['user']?>" size="50" onChange="required()">
<img src="images/blank.jpg" width="15" height="15" border="1"> </p>
<p>Current Password: <strong><font color="#FF0000">*</font></strong><br>
<input name="pass" type="password" id="pass" size="50">
<img src="images/blank.jpg" name="currpass" width="15" height="15" border="1" id="currpass"> </p>
<p>New Password: <em>(Please only fill in if you wish to change your current
password)<br>
<input name="newpass1" type="password" id="newpass1" size="50">
</em></p>
<p>Confirm New Password: <em>(Only required if you filled in 'New Password'
above)</em><br>
<input name="newpass2" type="password" id="newpass2" size="50">
<img src="images/blank.jpg" name="confpass" width="15" height="15" border="1" id="confpass"></p>
<p>EMail Address: <strong><font color="#FF0000">*</font></strong><br>
<input name="email" type="text" id="email" value="<?=$_SESSION['email']?>" size="50" onChange="required()">
<img src="images/blank.jpg" name="email" width="15" height="15" border="1" id="email"></p>
<p>Gender:<br>
<?php
$gender = '<select name="gender">
<option value="male">Male
<option value="female">Female
<option value="disclosed">Disclosed
</select>';
$gender = str_replace('value="'.$_SESSION['gender'].'">', 'value="'.$_SESSION['gender'].'" selected>', $gender);
echo $gender;
?>
</p>
<p>
<input type="submit" name="Submit" value="Update Account Details" disabled="false"></p>
</div>
</form>

Bill Posters
10-22-2006, 01:44 PM
<script type="text/javascript" language="JavaScript">
function required() {
var check
if(document.updateaccount.user.value == "") {
check = 1;
}
if(document.updateaccount.email.text == "") {
check = 1;
}
if(check == 1) {
document.updateaccount.Submit.disabled = "true";
} else {
document.updateaccount.Submit.disabled = "false";
}
}
</script>

Checking for the disabled value returns a boolean value of either true or false. By placing true and false within quotes, you are referencing them as a 'string literal'.


function required() {
var check;
if ((document.updateaccount.user.value == "") || (document.updateaccount.email.text == "") {
check = 1;
}
if (check == 1) {
document.updateaccount.Submit.disabled = true;
} else {
document.updateaccount.Submit.disabled = false;
}
}

As the earlier check can also return a true/false boolean value, it could be referenced more directly, with the script written as follows…

function required() {
var check;
if ((document.updateaccount.user.value == "") || (document.updateaccount.email.text == "")) {
check = 1;
}
document.updateaccount.Submit.disabled = (check == 1);
}

You could even go a step further and use the first check to power the final value of the submit button's disabled value.

function required() {
document.updateaccount.Submit.disabled = ((document.updateaccount.user.value == "") || (document.updateaccount.email.text == ""));
}

HTH



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum