views:

192

answers:

4

The "onsubmit" statement is not called:

<form action="index.php" method="post" onsubmit="return validateSearchKeyword()">
     <input class="text_search" id="text_search" name="text_search" type="text" value="search" onfocus="if (this.value=='search') this.value = ''" onBlur="if (this.value=='') this.value = 'search'"  />
    </form>

This is my validation function:

function validateSearchKeyword()
{ alert ('sad');//for testing purposes - this alert is not showed
if (document.getElementById('text_search').value==""){creatediv('divWarnSearch','You must supply a value', '120px', '250px');return false;}
}

The function creatediv is working great with other forms, so the problem is not that one.

Any ideas? Thanks

+2  A: 

You're missing a submit button, which may result in users not being able to submit the form in some browsers.

Furthermore, it would be helpful if you could post your validation function, so we can check if there problem is there, e.g. does it return a boolean?

What are you results if you use the following:

<form ... onsubmit="alert('Foo'); return true">
David Grant
Do I really need the submit button to validate it? Because, when the Enter key is pressed, the form is correctly submitted. Thanks
Armadillo
@Armadillo, And how do you press the Enter key if you happen to be browsing on a device that doesn't have a keyboard?
LukeH
A: 

tried onsubmit="javascript: validateSearchKeyword();" ? AFAIK, it's already returning, no need to specify that.

You may also try removing the ().

And also, the submit button as Mr Potato Head says, if you didn't remove it to make the code terse.

EDIT: This was a badly thought out post. This is a good example of what not to do. If you find any of these suggestions implemented in your code, refactor!

Adriano Varoli Piazza
Actually, it is necessary to use return, as the return type of onsubmit dictates whether or not the form is submitted.
David Grant
I mean that if the function already returns inside its body, you'd be calling return twice.
Adriano Varoli Piazza
@Adriano: Exactly: you return once from validateSearchKeyword(), and once from onsubmit.
David Grant
Why the downmod? Not angry, would just like to know.And my comments meant 'I haven't had to do it with 'return function()'
Adriano Varoli Piazza
@Adriano: Downmod wasn't from me...
David Grant
Oh, I didn't mean it was you, I just asked.
Adriano Varoli Piazza
I didn't -1, but I see why someone did. 1) A 'javascript:' label is COMPLETELY unnecessary, 2) suggesting they remove () is silly, 3) A boolean should be returned from the validation function, which should be returned to the onsubmit event: true to allow the submission to occur, false to abort it.
Grant Wagner
Fair enough. I should have thought a bit before posting. Actually, this is a good example of bad practices. Marking it as such.
Adriano Varoli Piazza
A: 

Check to make sure that the function you are calling is actually there, then I'd go through that function to check for problems. Firebug might be of use to you, or you could just go with sticking the following in the top of the called function (to make sure it's getting that far):

alert('here');
Mr. Matt
Firefox's error console will already tell you if the function exists or not, no need for firebug for that.
Adriano Varoli Piazza
Yeah, but I was thinking more along the lines of stepping through the function to look for problems.
Mr. Matt
A: 

Your problem is :

onBlur="if (this.value=='') this.value = 'search'"

When you submit, you lost the focus on the textbox and the event is launched: try to put

alert(document.getElementById('text_search').value);

and you will get "search" and not empty string...

RVeur23
Good point, but the onsubmit event fires before the onblur refills the textbox (well, in FF3, IE7 and Chrome it does). The OP should probably update their validation function to ignore the "default" value though, just to be safe across all browsers.
LukeH
I'm at the job now and here we are in IE6. Just to be sure, add =='' || =='Search'...
RVeur23