JQuery Validation Plugin: Use Ajax Custom Method

Seek some help with the jQuery form validation plugin if possible.

I check the email field of my form for blur by making an ajax call in my database, which checks if the text in the email field is currently in the database.

    // Check email validity on Blur
       $('#sEmail').blur(function(){
       // Grab Email From Form
       var itemValue = $('#sEmail').val();
        // Serialise data for ajax processing
        var emailData = {
            sEmail: itemValue
        }
        // Do Ajax Call
         $.getJSON('http://localhost:8501/ems/trunk/www/cfcs/admin_user_service.cfc?method=getAdminUserEmail&returnFormat=json&queryformat=column', emailData, function(data){

                    if (data != false) {
                        var errorMessage = 'This email address has already been  registered';
                    }
                    else {
                        var errorMessage = 'Good'
                    }
                })
    });

What I would like to do is include this call in the rules of my jQuery validation plugin ... for example

    $("#setAdminUser").validate({
      rules:{
             sEmail: {
                  required: function(){
                  // Replicate my on blur ajax email call here
                 }                  
             },
            messages:{
                sEmail: {
                    required: "this email already exists"       

            }
        });

Want to know if there are any achievements? Many thanks

+3
source share
4 answers

You execute an AJAX request, ergo: the check is already completed when your custom validator returns true or false.

async. . : jQuery , Ajax?

, . , unqiue Mobile no:

jQuery.validator.addMethod("uniqueMobile", function(value, element) {
    var response;
    $.ajax({
        type: "POST",
        url: <YOUR SERVER SCRIPT/METHOD>,
        data:"mobile="+value,
        async:false,
        success:function(data){
            response = data;
        }
    });
    if(response == 0)
    {
        return true;
    }
    else if(response == 1)
    {
        return false;
    }
    else if(response == 'logout')
    {
        return false;
    }
}, "Mobile is Already Taken");
+15

,

 $.validator.addMethod('unique',
                     function(value) {
        $.get('http://localhost:8501/ems/trunk/www/cfcs/admin_user_service.cfc','?method=getAdminUserEmail&returnFormat=json&queryformat=column&emailData='+ value,
                            function(return_data) {
                                return return_data;
                            })
                         }
                         , "this email already exists"
                    );

unique -

$.validator.addClassRules("unique-email", {
                unique: true
            });
+1

:

$.validator.addMethod('unique', 
            function(value) {
         var result = $.ajax({ 
                             async:false, 
                             url:"http://yoursamedomain.com/checkemail",
                             data:'email='+ value, 
                             dataType:"text" }); 

         if(result .responseText) return true; else return false;

             } , "This email address has already been  registered");
+1

Use the rule to verify the correctness of AJAX on servers remote. This is the same as the standard jQuery AJAX request object. Your server should return the string "true" for a valid value or "false" for an invalid value, it can also pass some string "This message has already been accepted", and this will be perceived as invalid and display as an error message:

$( "#myform" ).validate({
  rules: {
    email: {
      required: true,
      email: true,
      remote: {
        url: "check-email.php",
        type: "post",
        data: {
          username: function() {
            return $( "#username" ).val();
          }
        }
      }
    }
  }
});

Read the remote-method documentation here

0
source

Source: https://habr.com/ru/post/1731187/


All Articles