문제

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