I have an invitation form that should only accept emails handled by GMail. I'm trying to plug in asynchronous validation, but not sure what should be returned from the server and what Javascript handlers to use.

  1. I would like to display "Not a GMail address" next to email input field if someone types a Yahoo email
  2. I would like to receive "Not a GMail address" message from the backend.

My attempt (not very good):

$('#invite').validate({
    submitHandler: function(form, e) {
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: {
                email: $(form).find('#email').val()
            },
            success: function() {debugger;},
            error: function() {alert('failed');}
        });

        return false;
    },
    invalidHandler: function(event, validator) {
        debugger;
    },
    rules: {
        email: {
          required: true,
          email: true,
          remote: {
            url: "/invite.php",
            type: "post",
            async: true,
            data: submittedEmail
        }
      }
    }
});

Thanks!

有帮助吗?

解决方案

problem solved.

Javascript code:

$('#invite').validate({
    rules: {
        email: {
            required: true,
            email: true,
            remote: {
                url: '/admin/isgmail/',
                type: 'POST',
                dataType: 'json',
                data: {
                    email: function() {
                        return $('#email').val();
                    }
                }
            }
        }
    },
    messages: {
        email: "GMail email is required."
    },
    onkeyup: false
});

invite.php should return string "true" if validation is successful, string "false" if there was an error OR JSON-encoded string describing the error: echo json_encode('This is not a Google account');.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top