简体   繁体   中英

jQuery Validation not validating

In my ASP.NET Web Application I'm trying to sending data from a User Control using jQuery Ajax call to WebMethod. So within the User Control I'm using HTML controls only.

Now I need to validate the input data before sending data with Ajax call. I tried using jQuery Validation Plugin but it does not work as Form validity is always true

HTML:

<input type="text" id="txtName" name="txtName" class="text-box" />

<button type="button" id="btnSubmitInquiry" class="button" value="Submit">
    Submit
</button>

Javascript :

$('#btnSubmitInquiry').click(function () {

    $('#Form1').validate({
        rules: {
            txtName: {
                required: true
            }
        }
    });

    var inquiry = {};
    inquiry.Name = $('#txtName').val();
    //more fields

    var DTO = { 'inquiry': inquiry };

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: '/Web/Site/AjaxHandler.aspx/Save',
        data: JSON.stringify(DTO),
        dataType: "json",
        success: function (data) {
            alert('success');
        } 

    });
});

How to make this work?

Had to add if ($("#Form1").valid()) before Ajax call

    if ($("#Form1").valid()) {

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            //....

        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM