使用If / Else语句的Jquery验证规则

时间:2014-09-09 00:20:23

标签: javascript jquery ajax jquery-validate validationrules

此代码有效,但我需要它基本上在id字段上执行If Else。如果id长度> 0,则不要根据需要检查名称,邮政编码。如果id不是> 0然后根据需要检查名称,邮政编码等。欢迎您的建议。谢谢,

 runAllForms();
 $(function () {
 $("#artist_create_event").validate({

        // Rules for form validation
        rules : {
            artist_create_event_name : {
                required : true
            },
            artist_create_event_desc : {
                required : true
            },
            id :  {
                required : true
             },
            name :  {
                required : true
             },
            zipcode :  {
                required : true
             },
            venue_name :  {
                required : true
             },
            city :  {
                required : true
             },
            state :  {
                required : true
             },
            location :  {
                required : true
             },
            event_address : {
                required : true
            }
        },

        // Messages for form validation
        messages : {
            artist_create_event_name : {
                required : 'Enter Event Name'
            },
            artist_create_event_desc : {
                required : 'Enter Event Description'
            },
            name : {
                required : 'Enter Venue Name'
            },
            zipcode : {
                required : 'Please enter Zipcode/Postalcode'
            },
            venue_name : {
                required : 'Enter Venue Name'
            },
            city : {
                required : 'Please enter City'
            },
            state : {
                required : 'Please enter State'
            },
            location : {
                required : 'Please enter Country'
            },
            event_address : {
                required : 'Please enter Address'
            }
        },

 //  Do not change code below
        errorPlacement : function(error, element) {
            error.insertAfter(element.parent());
        },
    // Ajax form submition
   submitHandler: function() {
    $('#artist_create_event').hide(0);
    $('#art_event_message').hide(0);
    $.ajax({
        url : 'artist_update_event.php',
        type : 'POST',
        dataType : 'json',
        data: {
            artist_create_event_name: $('#artist_create_event_name').val(),
            artist_create_event_desc: $('#artist_create_event_desc').val(),
            name: $('#name').val(),
            city:  $('#city').val(),
            state:  $('#state').val(),
            location:  $('#location').val(),
            zipcode:  $('#zipcode').val(),
            event_address : $('#event_address').val(),
            event_address2 : $('#event_address2').val()
        },
        success : function(data){
            $('#art_event_message').removeClass().addClass((data.error === true) ? 'error' :     'success')
                .text(data.msg).show(500);
            if (data.error === true) {
                if (data.goto == 1)       {
 delete json;
                }
                else {
                $('#artist_create_event').show(500);
delete json;
                }
                 }
            if (data.error === false) {

                $('#artist_create_event').show(500);
delete json;
                }
                },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            $('#art_event_message').removeClass().addClass('error')
alert('The error was: '+errorThrown);
alert('The error was: '+XMLHttpRequest);
alert('The error was: '+textStatus);
//                  .text('response.Text').show(500);
            $('#artist_create_event').show(500);
        }
    });

    return false;
    }

    });

});

2 个答案:

答案 0 :(得分:2)

您可以将必需值​​传递给下面给出的函数,假设id是输入字段的名称

var $id = $('input[name="id"]')
$("#artist_create_event").validate({

    // Rules for form validation
    rules: {
        artist_create_event_name: {
            required: true
        },
        artist_create_event_desc: {
            required: true
        },
        id: {
            required: true
        },
        name: {
            required: function () {
                return $id.val().length > 0;
            }
        },
        zipcode: {
            required: function () {
                return $id.val().length > 0;
            }
        },
        venue_name: {
            required: true
        },
        city: {
            required: true
        },
        state: {
            required: true
        },
        location: {
            required: true
        },
        event_address: {
            required: true
        }
    },

    // Messages for form validation
    messages: {
        artist_create_event_name: {
            required: 'Enter Event Name'
        },
        artist_create_event_desc: {
            required: 'Enter Event Description'
        },
        name: {
            required: 'Enter Venue Name'
        },
        zipcode: {
            required: 'Please enter Zipcode/Postalcode'
        },
        venue_name: {
            required: 'Enter Venue Name'
        },
        city: {
            required: 'Please enter City'
        },
        state: {
            required: 'Please enter State'
        },
        location: {
            required: 'Please enter Country'
        },
        event_address: {
            required: 'Please enter Address'
        }
    },

    //  Do not change code below
    errorPlacement: function (error, element) {
        error.insertAfter(element.parent());
    },
    // Ajax form submition
    submitHandler: function () {
        $('#artist_create_event').hide(0);
        $('#art_event_message').hide(0);
        $.ajax({
            url: 'artist_update_event.php',
            type: 'POST',
            dataType: 'json',
            data: {
                artist_create_event_name: $('#artist_create_event_name').val(),
                artist_create_event_desc: $('#artist_create_event_desc').val(),
                name: $('#name').val(),
                city: $('#city').val(),
                state: $('#state').val(),
                location: $('#location').val(),
                zipcode: $('#zipcode').val(),
                event_address: $('#event_address').val(),
                event_address2: $('#event_address2').val()
            },
            success: function (data) {
                $('#art_event_message').removeClass().addClass((data.error === true) ? 'error' : 'success')
                    .text(data.msg).show(500);
                if (data.error === true) {
                    if (data.goto == 1) {
                        delete json;
                    } else {
                        $('#artist_create_event').show(500);
                        delete json;
                    }
                }
                if (data.error === false) {

                    $('#artist_create_event').show(500);
                    delete json;
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('#art_event_message').removeClass().addClass('error')
                alert('The error was: ' + errorThrown);
                alert('The error was: ' + XMLHttpRequest);
                alert('The error was: ' + textStatus);
                //                  .text('response.Text').show(500);
                $('#artist_create_event').show(500);
            }
        });

        return false;
    }

});

答案 1 :(得分:0)

假设id是文本框的id。你可以这样做:

    name :  {
            required : $("#id").val().length <= 0
         },

    zipcode :  {
            required : $("#id").val().length <= 0
         },