IE中的Jquery表单提交在提交之前未经过验证

时间:2011-08-12 15:32:30

标签: jquery forms internet-explorer submit

以下jquery脚本我使用。但这适用于firefox和chrome,但不适用于IE。

<script type="text/javascript">
$.validator.setDefaults({
submitHandler: function() {
        //alert("submitted!");
        debug: false;
    form.submit();
    }
});;
</script>
<script type="text/javascript">
$(document).ready(function() {
                   $.validator.addMethod("mobilecheck", function(value, element) {
                        return this.optional(element) || /^[0-9]+$/i.test(value);
                    }, "mobile number  must contain only numbers");
                $.validator.addMethod("namecheck", function(value, element) {
                        return this.optional(element) || /^[a-z\s]+$/i.test(value);
                    }, "User Name must contain only letters");

                    $("#myform").validate({
                                rules: {
                                                fullname:{ required : true , namecheck :true},

                                                mobile:{ required : true, mobilecheck :true },

                                                city: { required : true},


                                                locality: { required : true},

                                                email: {
                                                                required: true,
                                                                email: true


                                                },
                                                jobcity: { required : true} ,
                                                hometown: { required : true} ,

                                },

                                messages: {
                                                fullname:{
                                                                required: "Please enter your fullname" ,
                                                                namecheck: " Full Name must contain only alphabets<br/>"
                                                                },

                                                mobile: {
                                                                required: "Please enter mobile number",
                                                                mobilecheck: "Mobile number must contain only numbers "

                                                },

                                                city: "Please enter your current city" ,
                                                jobcity:"please select city" ,
                                                locality:"please enter locality where you are staying",


                                                email: {
                                                email:"Please enter a valid email address",
                                                required:"please enter email address"
                                                },

                                },

                            errorElement: "div"


                });


});

</script>

我的表单代码是

<form id="myform" method="post" class="register" >
    <ul>
    <li>
    <label for="fullname">Full Name</label>



    <input id="fullname" name="fullname" class="required namecheck" size="20" style="width:180px"/>


    </li>
    <li><label for="email">Email</label>

    <input id="email" name="email" class="required email" size="20" style="width:180px" />

    </li>
    <li>
    <label for="mobile">Mobile</label>

    <input id="mobile" name="mobile" class ="required mobilecheck" size="20" style="width:180px" maxlength="10"/>
    <span id="msgbox" style="display:none"></span>


    </li>
    <li>
        <label for="qualification">Qualification</label>

        <input id="qualification" name="qualification" class ="required " size="20" style="width:180px" maxlength="10"/>
        <span id="msgbox" style="display:none"></span>


    </li>
    <li>
    <label for="hometown">Home Town</label>

    <input id="hometown" name="hometown"  class="required" size="20" style="width:180px"/>


    </li>
    <li>
    <label for="city">Current City</label>

    <input id="city" name="city" size="20" class="required" style="width:180px"/>


    </li>
    <li>
    <label for="locality">Locality/Neighborhood/Area</label>

    <input id="locality" name="locality" class="required" size="20" style="width:180px"/>


    </li>

    <li>
        <label for="jobcity" >Preferred Location for Interview</label>

        <select name="jobcity" id="jobcity" clas="required">
                                <option value=""> -- please select-- </option>
                                <option value="Bangalore"> Bangalore</option>
                                <option value="Chennai">Chennai</option>
                                <option value="Gurgaon">Gurgaon/Noida/Delhi</option>
                                <option value="Hyderabad"> Hyderabad</option>
                                <option value="Mumbai">Mumbai</option>
                                <option value="Pune">Pune</option>

        </select></br>


    </li>


    </ul>


    <div style="text-align:center;margin-top:5px;padding-bottom:10px">
        <input name="hidden" type="hidden" value="hidden"/>
    <input class="blue1 button1" id="#submit" type="submit" value="Submit"/>

    </div>

当我在IE中提交表单时,它没有验证但是提交表单。在chrome和IE .jquery中,在提交之前验证表单。

我从代码中删除了不需要的逗号

1 个答案:

答案 0 :(得分:0)

删除以下属性中的尾随commas,它将正常工作。检查hometown中的rulesemail最后有messagescomma的{​​{1}}。这适用于除IE之外的所有其他浏览器。

        rules: {
            fullname: { 
                required: true, namecheck: true }, mobile: { required: true,
                mobilecheck: true
            },
            city: { required: true },
            locality: { required: true },
            email: {
                required: true, email: true
            },
            jobcity: { required: true }, 
            hometown: { required: true }
        },
        messages: {
            fullname: { 
                required: "Please enter your fullname", 
                namecheck: " Full Name must contain only alphabets<br />"
            },
            mobile: {
                required: "Please enter mobile number",
                mobilecheck: "Mobile number must contain only numbers " 
            },
            city: "Please enter your current city",
            jobcity: "please select city", 
            locality: "please enter locality where you are staying",
            email: {
                email: "Please enter a valid email address",
                required: "please enter email address"
            }
        }