Page 1 of 1

prevent submit

Posted: Sun Nov 02, 2008 10:10 am
by alexandruc
I have the following code:

Code: Select all

 
<SCRIPT language="JScript" type="text/javascript">
function IsEmpty( in_string ) {
   if( in_string.length == 0 || in_string == null ) return true;
   else return false;
}
 
function ValidateForm()
{
 if( IsEmpty( document.forms["name"].a.value ) ) {
    alert( 'enter at least 1 word' );
    return false;
  }
 if( ( document.forms["name"].a.value ) == "search" ) {
    return false;
  }
 
document.forms["name"].submit();
return true;
}
</script>
 
<form method="post" name="name" id="name">
<input name="a" id="a" type="text" value="search" onclick="if (value=='search') value=''";>
<input type="button" name="buton" value="search" onclick="ValidateForm();">
</form>
 
the code above is meant to prevent a user to submit a search without fiilling at leats 1 word into the form.

it works as intended as long as the button is clicked. If the user just presses "enter" the form is submited even if it is empty.

how can I prevent the "enter" button on the keyboard to submit the form?

Re: prevent submit

Posted: Sun Nov 02, 2008 9:56 pm
by novice4eva

Re: prevent submit

Posted: Mon Nov 03, 2008 4:04 am
by s.dot
Instead of using the onclick() javascript event handler, use onsubmit()..

Code: Select all

<form onsubmit="ValidateForm();" ...>
This will validate the form whether it was clicked or enter was pressed. Make sure you don't rely on the javascript, though. It should only be there for convenience to the end user.

Re: prevent submit

Posted: Mon Nov 03, 2008 11:05 am
by alexandruc
thanks for the reply, but neither solution works. :|

@scottayy:
I dont relay on the java.. i just want users to actually type something before submitting... :)

if I replace onclick with onsubmit, clicking the button wont trigger the submit, but clicking "enter" will still submit the form even if it's empty.

Basically if I replace onclick with on submit, validateform() is not triggered.

EDIT: i managed to make it work using code from novice4eva link. thanks for the help :)

Re: prevent submit

Posted: Mon Nov 03, 2008 12:25 pm
by kaszu
There must be return in the onsubmit, otherwise it just checks, but doesn't prevents the form from submitting:

Code: Select all

onsubmit="return ValidateForm();"
Code:

Code: Select all

<script type="text/javascript">
function IsEmpty( in_string ) {
   if( in_string.length == 0 || in_string == null ) return true;
   else return false;
}
 
function ValidateForm()
{
    if( IsEmpty( document.forms["name"].a.value ) ) {
        alert( 'enter at least 1 word' );
        return false;
    }
    if( ( document.forms["name"].a.value ) == "search" ) {
        return false;
    }
 
    return true;
}
</script>
 
<form method="post" name="name" onsubmit="return ValidateForm();" id="name">
    <input name="a" id="a" type="text" value="search" />
    <input type="submit" name="buton" value="search" />
</form>

Re: prevent submit

Posted: Mon Nov 03, 2008 12:37 pm
by alexandruc
thank you kaszu. now, using onSubmit works and it's a more simple method then detecting the onkeypress.

thank you all for the help :drunk: