【问题标题】:Issue with jQuery validate not working against elementjQuery validate 的问题不适用于元素
【发布时间】:2016-09-09 23:20:45
【问题描述】:

我在对当前项目中的表单使用 jQuery 验证时遇到问题。

我确定这是我遗漏的拼写错误或一些小问题,但无法弄清楚它发生的原因。

我在控制台调试器中遇到的错误是:对象不支持属性或方法“验证”

捆绑配置文件:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        bundles.Add(new ScriptBundle("~/bundles/custom").Include(
                    "~/Scripts/ContactForm.js"));

sn-ps代码如下:

<form action="@Url.Action("UpdateContactInformation", "ContactController")"  method="post" role="form" class="form-horizontal" id="contactForm">
<input type='hidden' name='csrfmiddlewaretoken' value='brGfMU16YyyG2QEcpLqhb3Zh8AvkYkJt' />
<!-- First Name Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">First Name</label>
    <div class="col-md-4">
        <input class="form-control" id="id_firstName" maxlength="75" name="txtFirstName" placeholder="First Name" required="required" title="" type="text" />
    </div>
</div>
<!-- Last Name Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">Last Name</label>
    <div class="col-md-4">
        <input class="form-control" id="id_lastName" maxlength="75" name="txtlastName" placeholder="Last Name" required="required" title="" type="text" />
    </div>
</div>
<!-- Title Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">Title</label>
    <div class="col-md-4">
        <input class="form-control" id="id_title" maxlength="75" name="txtTitle" placeholder="Title" required="required" title="" type="text" />
    </div>
</div>
<!-- Address Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">Address</label>
    <div class="col-md-4">
        <input class="form-control" id="id_address" maxlength="75" name="txtAddress" placeholder="Address" required="required" title="" type="text" />
    </div>
</div>
<!-- City Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">City</label>
    <div class="col-md-4">
        <input class="form-control" id="id_city" maxlength="75" name="txtCity" placeholder="City" required="required" title="" type="text" />
    </div>
</div>
<!-- State Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">State</label>
    <div class="col-md-4">
    <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenuStates" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
            Select State
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" id="statesDropDownMenu" aria-labelledby="dropdownMenuStates">

        </ul>
    </div>
        </div>
</div>
<!-- Zip Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">ZipCode</label>
    <div class="col-md-4">
        <input class="form-control" id="id_zipCode" maxlength="75" name="txtZipCode" placeholder="ZipCode" required="required" title="" type="number" />
    </div>
</div>
<!-- Email Primary Form Field-->
<div class="form-group required">
    <label class="col-md-2 control-label">Email Primary</label>
    <div class="col-md-4">
        <input class="form-control customEmail" id="id_emailPrimary" maxlength="75" name="txtEmailPrimay" placeholder="Email Primary" required="required" />
    </div>
</div>
<!-- Email Secondary (optional) Form Field-->
<div class="form-group">
    <label class="col-md-2 control-label">Email (Optional)</label>
    <div class="col-md-4">
        <input class="form-control" id="id_emailSecond" maxlength="75" name="txtEmailSecond" placeholder="Email (Optional)"  title="Email (Optional)" type="email" />
    </div>
</div>
<!-- Email Third (optional) Form Field-->
<div class="form-group">
    <label class="col-md-2 control-label">Email (Optional)</label>
    <div class="col-md-4">
        <input class="form-control" id="id_emailThird" maxlength="75" name="txtEmailThird" placeholder="Email (Optional)" title="Email (Optional)" type="email" />
    </div>
</div>

<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-primary">
            <span class="glyphicon glyphicon-user"></span> Submit Contact Info
        </button>
    </div>
</div>

@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval"); @Scripts.Render("~/bundles/custom"); //包含我要添加 $.validate.AddMethod() 到的文件

这是 Contact.js 的代码

$.validator.addMethod(
         "customEmail",
         function (value, element) {
             var re = new RegExp("/^@{0,2}\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*@{0,2}​‌‌​​$/");
             return this.optional(element) || re.test(value);
         },
         "Please enter a valid email address."
    );

$(document).ready(function () {

    console.log("Were here.........");

   // populateStatesDropDown();



    $('#contactForm').validate({ // initialize the plugin
        rules: {
            txtZipCode: {
                required: true,
                numeric: true
            },
            txtEmailPrimay: {
                required: true,
                customEmail:true
            },
            txtEmailSecond:{
                required:false,
                customEmail:true,
            },
            txtEmailThird: {
                required: false,
                customEmail:true
            }
        }
    });



    populateStatesList();

});

function populateStatesList() {
    var url = "Contact/GetStates"; // Don't hard code your url's!
    //$("#province_dll").change(function () {
    var $statesDropDownMenu = $("#statesDropDownMenu"); // Use $(this) so you don't traverse the DOM again
    var listItems = '';
        $.getJSON(url, function (response) {
            $statesDropDownMenu.empty(); // remove any existing options
            console.log(response);
            $.each(response, function (index, item) {
                console.log("Now - " + item);

                listItems += "<li>" + item + "</li>";                
            });

            $statesDropDownMenu.html(listItems);
        });
    //});
}

【问题讨论】:

  • 听起来jquery.validate.min.js 没有加载到您的页面上。检查您的网络选项卡并确保您看到该文件的状态为 200。或者您的 &lt;form id="contactForm"&gt; 声明中可能有语法错误。检查您的源代码并确保您的 ASP 正在生成预期的 HTML。
  • 是的,我检查了在我的 Contact.js 文件加载之前已经加载了广告。不过谢谢。我在加载包含 jquery.validate.js 的包后加载它
  • 调试器显示它通过了 contact.js 文件,因此通过了要添加的 $.validator 特殊方法,但尝试在 $('#contactForm').validate 处初始化插件时失败({ }) 行。
  • console.log($('#contactForm').length); 产生什么?
  • 它返回 1 的值。我还想知道它在运行时是否不存在。这可能意味着无法验证表单内的元素,因此它在显示它正在初始化“验证”方法的行上失败......

标签: javascript jquery jquery-validate


【解决方案1】:

你有一个额外的逗号。

txtEmailSecond:{
        required:false,
        customEmail:true, // Here
},

【讨论】:

  • 啊,让我看看。我担心这是一种俯瞰。
  • Javascript 没有这样的尾随逗号问题。如果我看到两个逗号,那么它会产生一个意外的令牌错误。
  • 我在回复后马上想到了这一点。让我更新我的问题以显示捆绑配置页面,看看为什么它不会按预期顺序加载。
  • 正如@MonkeyZeus 所说,额外的逗号不是问题。
猜你喜欢
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多