JQuery Validation Email Regex not working -
i trying build validation system in jquery without using plugins
my jquery code such, have validation function each input, e.g.:
function isemail(email) { var regex = /^([a-za-z0-9_\.\-\+])+\@(([a-za-z0-9\-])+\.)+([a-za-z0-9]{2,4})+$/; alert("email "+regex.test(email)); return regex.test(email); }
you notice have alert in one, because email not validating despite regex being borrowed highly-reputed answer -> email validation using jquery
i tested several valid emails no avail. valid emails taken here -> http://en.wikipedia.org/wiki/email_address#valid_email_addresses. changed regex /\s+/ , else worked know problem lies within regex.
anyways, why regex not evaluate true? understanding is:
/^([a-za-z0-9_\.\-\+])+
begins character a-z, a-z, 0-9, ., -, or +. 1 or more times
\@
followed "@" symbol
(([a-za-z0-9\-])+\.)+
followed 1 or more of 1 or more characters a-z,a-z,0-9, or -, period "."
([a-za-z0-9]{2,4})+$
ending 1 or more of two, three, or 4 characters a-z,a-z,0-9
edit
i have news, there strange behaviour going on here. @ jsfiddle copied , pasted code -> http://jsfiddle.net/hxcd3/. email evaluates true. if visit website working on -> danmacmill.host22.com, , type same thing contact email input, displays false.
you notice logged email variable passed jquery in console can see exact string. why not validating question. reference, here validation checker code:
function check(isvalid, name, message) { if (! isvalid(name) ) { if (! $('#div-'+name).hasclass("warned") ) { $('#div-'+name).prepend('<div class="bubble-wrapper"><div class="bubble"><p>'+message+'</p></div></div>'); var div = $('#div-'+name).position(); $('#info p:first-child').text("top: "+div.top); $('#info p:nth-child(2)').text("left: "+div.left); $('#div-'+name+' .bubble-wrapper').css({ top: div.top - 35 + "px" }); $('#div-'+name).addclass("warned"); } return false; } else return true; }
the regex mentioned --which well known way, match 99% of emails.
the reason why believe it's not working you feeding function emails surrounding space or spacing, must trim emails before testing them.
-- edit --
your script (myscript.js:170) has error:
if (! check(isemail, "email", "invalid email.") ) {
remove quotes around email variable
if (! check(isemail, email, "invalid email.") ) {
same thing lines: 159 , 171
Comments
Post a Comment