开发者

What's wrong with this javascript function?

开发者 https://www.devze.com 2023-01-08 20:20 出处:网络
I am using the following javascript regex email validate function but it doen\'t seem to work why....

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开发者_C百科}\.)|(([\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]$/

0

精彩评论

暂无评论...
验证码 换一张
取 消