-1

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?

6
  • 1
    What do you mean by "doesn't work"? False positive or false negative? Commented Jul 27, 2010 at 10:52
  • @KennyTM it doesn't return true for a valid email id Commented Jul 27, 2010 at 10:54
  • That "filter" is most likely wrong also. Commented Jul 27, 2010 at 10:54
  • If your house has a hole in the roof, do you call the maintenance guy and say "my house doesn't work"? How about being a bit more specific about how it fails to work exactly. Commented Jul 27, 2010 at 10:54
  • @Matti i ve editted my question... Commented Jul 27, 2010 at 10:56

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Have you checked the value of document.getElementById("ctl00_TxtEmailId").value? I don't have problems with this snippet.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.