javascript - Need help checking if a value exist using JQuery .inArray() -
i display error message when user types value in input field (emailaddressval) matches value in array (invalidemailaddresses). don't know how go it. help!
$(document).ready(function(){ $("input[name='emailaddress']").blur(function(){ // actual email validation function var emailreg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; var haserror = false; var emailaddressval = $("input[name='emailaddress']").val(); var invalidemailaddresses = ['aol.com', 'yahoo.com', 'yahoo.fr', 'juno.com', 'hotmail.com', 'gmail.com']; if($.inarray(invalidemailaddresses,emailaddressval) == -1) { alert('the email provided not business related domain'); } }); });
try following code
$(document).ready(function () { $("input[name='emailaddress']").blur(function () { var emailaddress = $("input[name='emailaddress']").val().trim(); if (isvalidemailaddres(emailaddress)) { var haserror = false; var emailaddressval = emailaddress.split('@').slice(1)[0].trim(); var invalidemailaddresses = ['aol.com', 'yahoo.com', 'yahoo.fr', 'juno.com', 'hotmail.com', 'gmail.com']; if ($.inarray(emailaddressval, invalidemailaddresses) >=0) { alert('the email provided not business related domain'); } } else alert("invalid emailid"); }); function isvalidemailaddres(emailid) { var regexexp = new regexp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|\d|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.)+(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])|(([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])([a-z]|\d|-|\.|_|~|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])*([a-z]|[\u00a0-\ud7ff\uf900-\ufdcf\ufdf0-\uffef])))\.?$/i); return regexexp.test(emailid); } });
see fiddle changes did
i added email validation function. if valid email address proceed hostname checking.
correct format of $.inarray
$.inarray( stringvalue, array)
. has $.inarray(emailaddressval,invalidemailaddresses)
emailaddressval contain full email address. first need extract hostname full email address. done @ var emailaddressval = emailaddress.split('@').slice(1)[0].trim();
if email address abc@gmail.com
, above line return gmail.com
.
return type of $inarray position @ passed string (emailaddressval in case) present in arraylist (invalidemailaddresses). return value greater or equal 0. per requirement have show message when want host in email id present in array of host names. need if($.inarray(emailaddressval, invalidemailaddresses) >=0)
Comments
Post a Comment