...

View Full Version : Submit button disable and enable upon correct validation



deadlydragon121
09-20-2011, 12:14 PM
Hi, I am having a problem with my script used to disable my submit button. I want the button to disable upon correct submission of the form, and the button to be kept enabled if the required fields are not filled in. I have the following code at the moment (which only does the first if statement).



SCRIPT CODE:

<script type="text/javascript" >
function validateForm()
{

var amount=document.forms["expenserecord"]["amount"].value;
var picker=document.forms["expenserecord"]["picker"].value;
var desc=document.forms["expenserecord"]["description"].value;

if (amount==null || amount=="" || picker==null || picker=="" || desc==null || desc=="") {
alert("All required fields must be filled out.");
document.getElementById("sub").disabled = false;
return false;
}

else {
document.getElementByID("sub").disabled = true;
}

}

</script>

BUTTON CODE:

<p><input type="submit" value="Submit" onClick="return validateForm()" id="sub"></p>

Kor
09-20-2011, 12:53 PM
Unless you are performing a submit to another document using as target a hidden iframe (which is subtle technique of uploading without changing the main document's session), the submit action will change the session anyway, thus any javascript change you could have done before is lost.

You may, probably use a cookie (client or server side) to solve that problem. Or you may use AJAX to submit data, which will allow you to keep the session.

http://en.wikipedia.org/wiki/Ajax_%28programming%29

deadlydragon121
09-20-2011, 12:58 PM
Unless you are performing a submit to another document using as target a hidden iframe (which is subtle technique of uploading without changing the main document's session), the submit action will change the session anyway, thus any javascript change you could have done before is lost.

You may, probably use a cookie (client or server side) to solve that problem. Or you may use AJAX to submit data, which will allow you to keep the session.

http://en.wikipedia.org/wiki/Ajax_%28programming%29

I solved the issue by changing the button to have an onChange() value referring to the script also. Thanks anyway.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum