$.fn.mailgun_validator = function(options) {
return this.each(function() {
$(this).focusout(function() {
run_validator($(this).val(), options);
});
});
};
function run_validator(address_text, options) {
// don't run validator without input
if (!address_text) {
return;
}
// length check
if (address_text.length > 512) {
error_message = 'Stream exceeds maxiumum allowable length of 512.';
if (options && options.error) {
options.error(error_message);
}
else {
console.log(error_message);
}
return;
}
// validator is in progress
if (options && options.in_progress) {
options.in_progress();
}
// require api key
if (options && options.api_key == undefined) {
console.log('Please pass in api_key to mailgun_validator.')
}
var success = false;
// make ajax call to get validation results
$.ajax({
type: "GET",
url: 'https://api.mailgun.net/v2/address/validate?callback=?',
data: { address: address_text, api_key: options.api_key },
dataType: "jsonp",
crossDomain: true,
success: function(data, status_text) {
success = true;
if (options && options.success) {
options.success(data);
}
},
error: function(request, status_text, error) {
success = true;
error_message = 'Error occurred, unable to validate address.';
if (options && options.error) {
options.error(error_message);
}
else {
console.log(error_message);
}
}
});
// timeout incase of some kind of internal server error
setTimeout(function() {
error_message = 'Error occurred, unable to validate address.';
if (!success) {
if (options && options.error) {
options.error(error_message);
}
else {
console.log(error_message);
}
}
}, 30000);
}
Linkedin Profile Here is creative java developer.having many creative concept to solve spring complexity
Wednesday, July 16, 2014
mailgun_validator.js for email validation
email validation
Here we use an "mailgun_validator.js" for email validation .
//Html Code
<form>
<div>
<label for="email">Email address</label><br>
<input type="email" name="email" id="email">
<div id="status"></div>
</div>
<div>
<button type="submit" id="validate_submit">Validate</button>
</div>
</form>
//java script
<script src="mailgun_validator.js"></script>
// document ready
$(function() {
// capture all enter and do nothing
$('#email').keypress(function(e) {
if(e.which == 13) {
$('#email').trigger('focusout');
return false;
}
});
// capture clicks on validate and do nothing
$("#validate_submit").click(function() {
return false;
});
// attach jquery plugin to validate address
$('#email').mailgun_validator({
api_key: 'MAILGUN_PUBKEY', // replace this with your Mailgun public API key
in_progress: validation_in_progress,
success: validation_success,
error: validation_error,
});
});
// if email successfull validated
function validation_success(data) {
$('#status').html(get_suggestion_str(data['is_valid'], data['did_you_mean']));
}
// if email is invalid
function validation_error(error_message) {
$('#status').html(error_message);
}
// suggest a valid email
function get_suggestion_str(is_valid, alternate) {
if (alternate) {
return '<span class="warning">Did you mean <em>' + alternate + '</em>?</span>';
} else if (is_valid) {
return '<span class="success">Address is valid.</span>';
} else {
return '<span class="error">Address is invalid.</span>';
}
}
MAILGUN_PUBKEY: pubkey-5ogiflzbnjrljiky49qxsiozqef5jxp7
Subscribe to:
Posts (Atom)