简体   繁体   中英

What's wrong with this javascript function?

I am using the following javascript regex email validate function but it doen't seem to work why....

function IsValidEmail(email) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return filter.test(email);
}

function forgetpassword() {
    if (document.getElementById("ctl00_TxtEmailId").value == "") {
        return false;// this condition gets exected so no prob with my txtboxID
    }
    if (document.getElementById("ctl00_TxtEmailId").value != "") {
        return IsValidEmail(document.getElementById("ctl00_TxtEmailId").value);
    }
    return true;
}

My failed inputs were test,test@test and also test@test.com

Guys my textbox is within a facebox modal popup.... when i tried alert(document.getElementById("ctl00_TxtEmailId").value with some text jsadf the alert displayed with nothing... Why?

I would change the regexp to something like

/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/

and I would rewrite forgetpassword to

function forgetpassword() {
    return IsValidEmail(document.getElementById("ctl00_TxtEmailId").value);
}

Edit : Complete function

function IsValidEmail(email) {
    var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
    return filter.test(email);
}

IsValidEmail('janus@aaa.bbb') -> true in Chrome / IE8

Want to try this?

/^[a-zA-Z][\\w.-] [a-zA-Z0-9]@[a-zA-Z0-9][\\w.-] [a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z]$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM