2

I am validating my form using jquery as below:

jQuery(document).ready(function(){
        jQuery('#adminform').validate({

        rules: {
          name: {
            minlength: 5,
            maxlength: 30,
            required: true
          },
          username: {
            required: true,
            minlength: 5,
            maxlength: 30
          }
        },
        highlight: function(label) {
            jQuery(label).closest('.control-group').addClass('error');
        },
        success: function(label) {
            label
                .text('OK!').addClass('valid')
                .closest('.control-group').addClass('success');
        },
        messages:{

          name: {
            required: "Enter your name"
          },
          username: {
            required: "Enter a username"
          } 
       }
      });     
});

now how should I prevent the form from submission if the rules are not meet?

When I click the submit button nothing should happen.

3
  • Take a look at your JS console and check for errors. Commented Dec 24, 2012 at 8:49
  • You are using a plugin called validate. Your code is incomplete. Please post the whole code so we could help Commented Dec 24, 2012 at 8:50
  • @Ahmad Alfy to be honest i have more fields out of which i have just shown two of them here. but i feel whether i have 2 or 10 fields, the code is just to add rules and the corresponding messages if invalid. so the code will look like above only. Commented Dec 24, 2012 at 9:04

4 Answers 4

2

To stop a form being submitted you use:

return false;

When you know the input is invalid.

Sign up to request clarification or add additional context in comments.

Comments

2

Prevent form submission using jQuery?

$('#myFormId').submit(function(event) {
     event.preventDefault();
 });

OR:

$('#myFormId').submit(function()
{
    return false;
});

1 Comment

i tried this way: jQuery('#adminform').submit(function(event) { event.preventDefault(); });
1

Prevent the submit event and remain on the screen

e.preventDefault();

what about this?

2 Comments

where and how to put the e.preventDefault();
i am getting something like this: {"error": "Shell form does not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.ShellForm object at 0x8f7f56c>, 'html_name': 'js_lib', 'label': u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 0x934d24c>, 'help_text': '', 'name': 'js_lib'}"}
0

Your code is perfect just a submit trigger needed : http://jsfiddle.net/uUdWN/

jQuery(document).ready(function() {
jQuery('#adminform').validate({
    rules: {
        name: {
            minlength: 5,
            maxlength: 30,
            required: true
        },
        username: {
            required: true,
            minlength: 5,
            maxlength: 30
        }
    },
    highlight: function(label) {
        jQuery(label).closest('.control-group').addClass('error');
    },
    success: function(label) {
        label.text('OK!').addClass('valid').closest('.control-group').addClass('success');
    },
    messages: {
        name: {
            required: "Enter your name"
        },
        username: {
            required: "Enter a username"
        }
    }
});


$("#adminform").submit(); // <-------------just triggered submit

});

1 Comment

i added jQuery("#adminform").submit(); but that didn't help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.