views:

74

answers:

2

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 [email protected]

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?

+1  A: 

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('[email protected]') -> true in Chrome / IE8

Janus Tøndering
What about `.museum`?
Gumbo
@Janus it didn't work...
Pandiya Chendur
Have you checked the value of document.getElementById("ctl00_TxtEmailId").value? I don't have problems with this snippet.
Janus Tøndering
@Janus i ve edited my question..
Pandiya Chendur
A: 

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]$/

Josh