Reputation: 2807
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.
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!
Upvotes: 1
Views: 2899
Reputation: 2807
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');
.
Upvotes: 2