PDA

View Full Version : don't submit form if input text is "enter text here"



ungovernable
01-03-2010, 09:04 PM
well title says it all

i have a search form, and the default value is "enter text here"

when the user click the submit button, i would like to check if the value of my text input is "enter text here" and if it is, then don't submit the form (popup a warning or something)

how can i do that?

thanks a lot!

gusblake
01-03-2010, 09:29 PM
<form onsubmit="return (this.fieldname.value=="enter text here"?false:true)"

Old Pedant
01-04-2010, 02:03 AM
No, Gus, once again you are using "..." *inside* of "...". Won't work.

In any case, you don't need the ? : operator.

Just this is enough:


<form ... onsubmit="return this.SomeField.value != 'enter text here';">
...
<input name="SomeField" value="enter text here" />
...
</form>

ungovernable
01-04-2010, 03:33 AM
ok thanks, but im not sure if i understand...

how can i display a popup error if the field is "enter text here" when submit button is pushed ?

randomuser773
01-04-2010, 04:26 AM
how can i display a popup error if the field is "enter text here" when submit button is pushed ?
You can avoid specifying the default text by using the field's .defaultValue property.
<form action='#' onsubmit='return hasUserText(this.SomeField)'>

<input name="SomeField" value="enter text here">
<input type=submit>

</form>

<script type='text/javascript'>

function hasUserText( field )
{
var isEntered = field.value != field.defaultValue && /\S/.test( field.value );

if( !isEntered )
alert( 'Please enter a value' );

return isEntered;
}
</script>

Philip M
01-04-2010, 08:39 AM
A small improvement:-


<input name="SomeField" value= "Enter text here" onfocus = "if (this.value == 'Enter text here') {this.value = '' }">