jQuery How to check if email address valid
The article will tell you how to check if the email address valid. An example of getting an email address then checks by jQuery included. The example supports multiple email addresses entered in one input.
Javascript function to check email
function wtp_valid_email_address( email_address ) {
var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
return pattern.test( email_address );
};
The above function will return false if the emailAddress is invalid.
Full JQuery file to check an email input which supports multiple email address
HTML codes:
<form id="yavfbpc_custom_buttons_form_ID" method="post" action="">
<input type="button" name="yavfbpc_custom_buttons_duplicate" id="yavfbpc_custom_buttons_duplicate_ID" value="Duplicate" class="button-primary" />
<input type="button" name="yavfbpc_custom_buttons_emailto" id="yavfbpc_custom_buttons_emailto_ID" value="Email To" class="button-primary" />
<input type="text" name="wtp_email_address_input" id="wtp_email_address_input_ID" value="" style="width:500px;" />
<span> seperate by comma(,)</span>
</form>
jQuery codes:
jQuery(document).ready( function($) {
$("#wtp_email_button_ID").click(function(){
var emails = $("#wtp_email_address_input_ID").val();
if( $.trim(emails) == "" ){
alert("Please enter email address");
$("#wtp_email_address_input_ID").focus();
return false;
}
var emails_array = emails.split(',');
if( emails_array && emails_array.length > 0 ){
for( i = 0; i < emails_array.length; i++ ){
if( wtp_valid_email_address(emails_array[i]) == false ){
alert( 'Invalid email address: ' + emails_array[i] );
$("#wtp_email_address_input_ID").focus();
return false;
}
}
}
$("#yavfbpc_custom_buttons_form_ID").submit();
});
function wtp_valid_email_address( email_address ) {
var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
return pattern.test( email_address );
}
});
Post a Comment