prevent submit
Posted: Sun Nov 02, 2008 10:10 am
I have the following code:
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?
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>
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?