zhuanzhou
zhuanzhou

Reputation: 2443

using jquery validate but prevent the form submit

the form is still submitted. if i want to use your code and add a step.that is if show the red text then preventing the form to submit?how do i do? thank you.

Here is my script:

jQuery(function($)
{
    if (!$("#edit-name").val() || !$("#edit-email").val() || !$("#edit-comment-body").val())
    {
        $("#edit-submit").click(function()
        {
            $('#edit-name').after('<span style="color:red;">请输入你的名字</span>');
            $('#edit-mail').after('<span style="color:red;">请输入你的邮箱</span>');
            $('#edit-comment-body label span').append('<span style="color:red;">请填写内容</span>');
        });
    }
});

Upvotes: 0

Views: 1994

Answers (3)

Vaibhav Gupta
Vaibhav Gupta

Reputation: 1612

validate the fields inside submit event like:

$('#formID').submit(function(){
      if ($("#edit-name").val()=='' || $("#edit-email").val()=='' || $("#edit-comment-body").val()==''){
        $('#edit-name').after('<span style="color:red;">è¯·è¾“å…¥ä½ çš„åå­—</span>');
        $('#edit-mail').after('<span style="color:red;">è¯·è¾“å…¥ä½ çš„é‚®ç®±</span>');
        $('#edit-comment-body label span').append('<span style="color:red;">请填写内容</span>');

        return false; // to stop form submission in any way

} });

Upvotes: 2

Salman Arshad
Salman Arshad

Reputation: 272096

Normally, you hook an event listener to the submit event of the form, from there you can return false.

$("form#foo").submit(function(){
    // validate
    return false;
});

Upvotes: 1

Razor Storm
Razor Storm

Reputation: 12336

on the .click(function()) give it a parameter e

    $("#edit-submit").click(function(e)
    {
        e.preventDefault();
        $('#edit-name').after('<span style="color:red;">请输入你的名字</span>');
        $('#edit-mail').after('<span style="color:red;">请输入你的邮箱</span>');
        $('#edit-comment-body label span').append('<span style="color:red;">请填写内容</span>');
    });

Upvotes: 1

Related Questions