[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


> 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:

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


Chris Marsh

More information about the thelist mailing list