Using the remote function of jquery validation

后端 未结 8 680
逝去的感伤
逝去的感伤 2020-12-16 03:12

I\'m trying to figure out how I can turn this:

$(\'#username\').blur(function(){
    $.post(\'register/isUsernameAvailable\', 
           {\"username\":$(\'#         


        
8条回答
  •  不思量自难忘°
    2020-12-16 03:40

    I realize this is old but I had a hard time getting this working as well and wanted to share what worked for me.

    Client-Side form validation code:

    $('#frmAddNew').validate({
    onkeyup: false,
    rules: {
        ADID: {
            required: true,
            alphanumeric: true,
            maxlength: 10,
            remote: {
                url: "ADIDValidation.cshtml",
                type: "post",
                dataType: "json",
                dataFilter: function (data) {
                    if (data) {
                        var json = $.parseJSON(data);
                        if (json[0]) {
                            return JSON.stringify(json[0].valid) /* will be "true" or whatever the error message is */
                        }
                    }
                },
                complete: function (data) {
                   /* Additional code to run if the element passes validation */
                    if (data) {
                        var json = $.parseJSON(data.responseText);
                        if (json[0]) {
                            if (json[0].valid === "true") {
                                $('#FirstName').val(json[0].FirstName);
                                $('#LastName').val(json[0].LastName);
                            }
                        }
                    }
                }
            }
        }
    },
    messages: {
        ADID: {
            required: "Please Enter an ADID of the Employee",
            alphanumeric: "The ADID Can Only Contain Letters and Numbers",
            maxlength: "ADID For User's Current Permissions Must Be 10 Characters or Less"
        }
    },
    submitHandler: function (form) {
        form.submit();
    },
    errorContainer: $('section.errorCon'),
    errorLabelContainer: $('ol', 'section.errorCon'),
    wrapper: 'li',
    showErrors: function (errors) {
        var error = this.numberOfInvalids();
        var message = error == 1
            ? 'You missed 1 field:'
            : 'You missed ' + error + ' fields:';
        $('section.errorCon h3').html(message);
        this.defaultShowErrors();
    }
    });
    

    Server-Side code for ADIDValidation.cshtml (I'm using Razor webpages):

    @{
    
    Layout = null;
    
    if(IsPost)
    {
        var db = Database.Open("");
        var sql = "";
        var strADID = Request["ADID"];
    
        var result = db.Query(sql, strADID);
        IEnumerable response;
    
        if(result.Any()) 
        {
            @* "true" indicates the element passes validation. Additional data can be passed to a callback function *@
            response = result.Select(x => new
            {
                valid = "true",
                FirstName = x.FirstName,
                LastName = x.LastName
            });
        }
    
        else
        {
            @* The element did not pass validation, the message below will be used as the error message *@
            response = new[] {
                new {valid = "The Employee's ADID '" + strADID.ToString() + "' Is Not Valid. Please Correct and Resubmit"}
            };
        }
    
        Json.Write(response, Response.Output);
    }
    }
    

提交回复
热议问题