[thelist] Stopping a user submitting a form from the address bar using JS.

Chris Marsh chris at webbtech.co.uk
Fri Dec 13 08:25:01 CST 2002


Craig

> I've got a form on a page that uses JavaScript to validate
> the input.  This form is submitted with a button calling a JS
> function: <input type="button" value="Submit"
> onClick="check(form,form.elements.length);">
>
> The button is not a 'submit' button because after the script
> has validated the input, the script itself submits the form.
> However, if I open up this page in a browser (IE6) and type
> 'javascript:document.forms[0].submit()' (without the '), it
> will bypass the validation and submit the form.  How can I stop this?

Why don't you  simply submit the form using a submit button and run
check(...) onsubmit. In which case you could actually have function
check(o) {} and call check(this); getting the length of the elements
collection within the validation function.

A brief example follows:

<html><head><title>Test</title>
<script>
function validate(o) {
  if(o.elements[0].value == "") {
    alert("Fill out the form, silly!");
    return false;
  }
  else {
    alert("Nicely done!");
    return true;
  }
}
</script>
</head><body>
<form method='post' onSubmit='return validate(this);'>
<input type='text' />
<input type='submit' value='Test' name='Test' />
</form>
</body></html>

Regards

Chris Marsh





More information about the thelist mailing list