Click to See Complete Forum and Search --> : activating submit button depending on field values


idiotbear
07-11-2003, 06:29 AM
hi

I want to have the submit button on my form disabled until a bunch of fields are filled out.

How do I express this? I'm fairly new to proper JS, so bear with me!

I was thinking along the lines of

function validate()

{
if document.forms[0].amount.value == "" ||
document.forms[0].account.value == "" ||
document.forms[0].startdate.value == "" ||
{
document.forms[0].submit.disabled = true }
else {
document.forms[0].submit.disabled = false }

}

I'm planning to call this on the onchange of each of the affected fields.

I know the above isn't right, but I don't know why - very inexperienced in JS logic and syntax.

I'd appreciate some help!

Charles
07-11-2003, 06:33 AM
Just have your validation function return 'false' if you don't want the form to be submitted and then use something like:

<form action="someScript.pl" onsubmit="return validate(this)">

idiotbear
07-11-2003, 06:37 AM
thanks, but I don't know how to do this at all. my problem is not how to call the function, but how to write the function in the first place.

Charles
07-11-2003, 06:51 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
-->
</style>
<script type="text/javascript">
<!--
function validate(f) {if (/\S/.test(f.fee.value) && /\S/.test(f.fie.value)) {return true} else {return false}}
// -->
</script>
<form action="someScript.pl" onsubmit="return validate(this)">
<div>
<label><input type="text" name="fee">Fee</label>
<label><input type="text" name="fie">Fie</label>
<button type="submit">Submit</button>
</div>
</form>