prevent submit

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
alexandruc
Forum Newbie
Posts: 9
Joined: Sat Oct 18, 2008 11:41 am

prevent submit

Post 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?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: prevent submit

Post by novice4eva »

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: prevent submit

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
alexandruc
Forum Newbie
Posts: 9
Joined: Sat Oct 18, 2008 11:41 am

Re: prevent submit

Post 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 :)
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: prevent submit

Post 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>
alexandruc
Forum Newbie
Posts: 9
Joined: Sat Oct 18, 2008 11:41 am

Re: prevent submit

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