单击submit:jquery时,验证不起作用

时间:2017-02-20 11:52:57

标签: jquery jquery-validate

我知道JQuery,我正在尝试验证一个简单的表单。

这里的问题是点击提交时验证不起作用。相反,我收到错误消息:



$(document).ready(function () {
    $("#form").validate({
        rules: {
            "name": {
                required: true,
                minlength: 5
            },
            "email": {
                required: false,
                email: true
            }
        },
        messages: {
            "name": {
                required: "Please, enter a name"
            },
            "email": {
                required: "Please, enter an email",
                email: "Email is invalid"
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form submitted'); // for demo
            return false; // for demo
        }
    });

});

<form id="form" method="post" action="#">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" />
    <label for="email">Email</label>
    <input type="email" name="email" id="email" />
    <button type="submit">Submit</button>
</form>
&#13;
&#13;
&#13;

这是jsfiddle:https://jsfiddle.net/aimanpanday87/hsdscmcj/

2 个答案:

答案 0 :(得分:2)

请将您的代码设置如下,或查看以下小提琴链接:

工作代码链接:https://jsfiddle.net/hsdscmcj/9/

refreshData function(){


    var rows = [];

   //NORMALLY HERE THERE WILL BE AN AJAX REQUEST TO GET DATA FROM SERVER

    var items = jsonData.driverScorecardItems;

    for (var i = 0; i < items.length; i++) {

      var item = items[i];

      // 
      var thisRow = [{
        v: new Date(item.dateFormatted),
        f: item.dateFormatted
      }, {
        v: item.harshBrakings
      }, {
        v: item.distanceTravelledKM
      }];


      rows.push(thisRow);
    }

    // Add the rows to my Data Table
    scorecardChartData.addRows(rows);

    // Redraw the Chart
    scorecardDashboard.draw(scorecardChartData);

}

这是有效的,请检查。

由于

答案 1 :(得分:2)

显示以下代码在邮政编码中编辑只添加js链接

$(document).ready(function () {
    $("#form").validate({
        rules: {
            "name": {
                required: true,
                minlength: 5
            },
            "email": {
                required: false,
                email: true
            }
        },
        messages: {
            "name": {
                required: "Please, enter a name"
            },
            "email": {
                required: "Please, enter an email",
                email: "Email is invalid"
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form submitted'); // for demo
            return false; // for demo
        }
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js"></script>
<form id="form" method="post" action="#">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" />
    <label for="email">Email</label>
    <input type="email" name="email" id="email" />
    <button type="submit">Submit</button>
</form>