Microsoft JScript运行时错误:无法获取属性“form”的值:object为null或undefined:Validator.js

时间:2013-11-27 07:07:55

标签: jquery jquery-validate

我正在使用jquery validate.js文件进行验证,如果我在我的应用程序中包含此文件,则会抛出标题错误。以下是我的代码

的Javascript

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

    $("#form1").validate({
        rules: {
            field: {
                required: true,
                maxlength: 4,
                customvalidation: true,
                regex: "^[a-zA-Z]+$"
            },


            messages: {
                field: {
                    required: "Please enter name",
                    maxlength: "Maximum length allowed 4"
                },

            }


        }
    );

    $.validator.addMethod("customvalidation",

        function (value, element) {
            return /^[A-Za-z\d]+$/.test(value);
        },
        "Sorry, no special characters allowed");


    $.validator.addMethod("regex", function (value, element, regexp) {
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    }, "Only Characters from A-z");

});

HTML

<form id="form1" runat="server">
      <h1>Registration Form</h1>

    <div>
       <div id="tabs-1" style="width: 100%;">
       <table>

       <tr>
       <td align="right">
         <span style="color: Red">* </span>
            <asp:Label ID="Label1" runat="server" Text="Employee Name: " ></asp:Label>
           </td>
           <td>
           <asp:TextBox ID="field" runat="server"  name="field"></asp:TextBox>

            </td>
      </tr> 
      <tr>
       <td align="right">
           <asp:Button ID="submit" runat="server" Text="Validate" />

           </td>
           </tr>

       </table>
                       </div>
    </div>
    </form>

注意:还有一个观察结果,当我从网站url访问js和css文件时,它工作正常。我想有些文件可能会丢失。请在这里提供帮助。

1 个答案:

答案 0 :(得分:0)

您的代码中存在严重的语法错误...

$("#form1").validate({
    rules: {
        field: {
            required: true,
            maxlength: 4,
            customvalidation: true,
            regex: "^[a-zA-Z]+$"
        },
        messages: { // <- this does not go inside of "rules" option
            field: {
                required: "Please enter name",
                maxlength: "Maximum length allowed 4"
            },  // <- extra comma
        }
    }
);  // <- missing closing brace "}"

1)您错误地将<{1}} 置于 messages内作为rules的兄弟姐妹。 fieldmessages的兄弟姐妹。

2)你最后错过了一个结束括号。应该是rules而不是});

3));中有一个逗号逗号。虽然草率,但这只是旧版Internet Explorer中的一个问题。

固定代码:

messages

<强>侧面说明:

根据the jQuery documentation,这是“不推荐”......

$("#form1").validate({
    rules: {
        field: {
            required: true,
            maxlength: 4,
            customvalidation: true,
            regex: "^[a-zA-Z]+$"
        }
    },
    messages: {
        field: {
            required: "Please enter name",
            maxlength: "Maximum length allowed 4"
        }
    }
});

确实应该更改为$().ready(function () { $(document).ready(function () {