views:

56

answers:

3

I am building a site which uses jQUery validation plugin and want things validated before submitting the form. My code looks like follows

<form>
  <input type="button" value="Submit the Form" onclick="validateAndSubmit()" />
</form>
<script language="javascript">

   function validateAndSubmit(){
   //do some validation and then submit 
 }

</script>

In Firefox, this works perfectly. In Chrome, when I hit enter anywhere in the page, the form submit is triggered and validation doesn't work either. Is there something to avoid this ? Shouldn't the browser not submit a form when we hit an enter if there is no submit button?

A: 

I'm not sure if there's a standard regarding this or not.

Regardless, you can save yourself the trouble altogether by simply adopting a stronger strategy: implement the validation as an onsubmit action on the form, rather than an onclick action for the button. I almost never use buttons in forms; having to do so for yours would only throw me off, and that's not good for users.

So anyway. Form onsubmit is the way to go. And I'd appreciate it if you used unobtrusive Javascript instead of the HTML attributes :)

Matchu
You users won't appreciate your page adding behaviour after it has loaded if they've already submitted the form before the page loads and missed out on helpful JavaScript functionality.
Tim Down
@Tim Down - I know it's a point of semantics, but the only reason they won't appreciate it is because they won't know it ever existed. However, it sounds like you use `window.onload` instead of DOMReady functions, which is the real source of the issue of having to wait on page load. (That is, unless the user's connection is so slow that it can't even download the script before they fill out a whole form, in which case directly embedding the Javascript will make things even worse for page load time, anyway.)
Matchu
I use `DOMContentLoaded` where available. I just don't buy into the idea that using event handler attributes is always fundamentally worse that assigning event handlers once the document has loaded.
Tim Down
@Tim Down - I've generally found from experience that keeping my Javascript all together keeps me better organized, and it seems that most of the web development industry agrees - and I know for sure that, if I were inheriting a project, I would want everything in one place. However, I suppose there are always exceptions... but generally those exceptions take special justification for me, such as being Google.
Matchu
I don't think we don't seriously disagree: I very rarely use event handler attributes for a variety of reasons, including the popular ones to do with separation of concerns. However, for a very simple application of JavaScript on a single page (for example, some simple validation on a single form), I think using attributes is positively the better approach.
Tim Down
+1  A: 

use this syntax:

<form onsubmit="return validateAndSubmit()">
...

if you need to catch the return-key maybe you can handle it by binding an keydown event to the input and perform some action on keyCode #13

helle
+1  A: 

Try this method:

<form onsubmit="return validateAndSubmit(this);">
  <input type="submit" value="Submit the Form"/>
</form>
<script language="javascript">

   function validateAndSubmit(form_obj){
     if(some_variable == 'correct_value') {
        form_obj.submit();
        return true;
     } else {
        alert('Wrong value');
        return false;
     }
   //do some validation and then submit 
 }

</script>
antyrat
What should the function validateAndSubmit() return? A boolean value? How do I stop the validation if I have to?
Ritesh M Nayak
I've updated the code. I hope it is clear now.
antyrat