使用jquery-form-validator进行表单验证

时间:2014-01-13 16:42:40

标签: jquery forms validation

jquery-form-validator插件不验证..我使用的是jQuery 1.9.0(该插件使用该版本进行了测试)。

HTML:

 <form id="Form" method="get" action="">
        <div class="box-1">
          <label for="name">CONTACT NAME*<br /></label>
          <label for="email">CONTACT EMAIL ADDRESS*<br /></label>
        <label for="tel">CONTACT PHONE NUMBER</label>
      </div>

      <div class="box-2">
        <input id="name" name="name" type="text" /><br />
        <input id="email" name="email" type="text" /><br />
        <input id="tel" name="tel" type="tel"/><br /><br />
        <input class="submit" type="submit" value="SEND" />
     </div>
     </form>

这里是我在head标签中添加的javascript(当然是脚本src到插件和jquery):

的javascript:

   <script>
    $.validator.setDefaults({
              submitHandler: function() { alert("submitted!"); }
            });
            $().ready(function() {
                $("#Form").validate({
                    rules: {
                        name: "required",
                        email: "required"
                    },
                    messages: {
                        name: "Please enter your name",
                        email: "Please enter your email";
                    }
            }); 
    </script>

出了什么问题?

2 个答案:

答案 0 :(得分:1)

更改自:

$()

$(document)

您还需要更改rules

rules: {
    name: { required: true },
    email: { required: true }
}

您还需要将所有代码都包装在DOM中。最终代码应如下所示:

$(document).ready(function() {
    $.validator.setDefaults({
        submitHandler: function() { alert("submitted!"); }
    });

    $("#Form").validate({
        rules: {
             name: { required: true },
             email: { required: true }
        },
        messages: {
             name: "Please enter your name",
             email: "Please enter your email";
        }

});

答案 1 :(得分:0)

试试这个:

rules: {
    name: { required: true },
    email: { required: true }
},