Forum Moderators: open

Message Too Old, No Replies

Need help with Email filter

please

         

Acternaweb

12:42 am on Apr 10, 2005 (gmt 0)

10+ Year Member



Here is my code so far, but I want to "reject" email address that have the wrong exchange, namely they should be .com, .gov or .net.

Please tell me how I can adapt my code for this.

Thanks,

if (document.feedbackform.email.value == "")
{
alert ( "Please enter your email." );
document.feedbackform.email.focus();return false;
}
{
if (document.feedbackform.email.value.indexOf("@")==-1
¦¦ document.feedbackform.email.value.indexOf(".")==-1 ¦¦
document.feedbackform.email.value.indexOf(" ")!=-1 ¦¦ document.feedbackform.email.value.length<6)
{alert("Sorry, your e-mail address is not valid. Please check '@''.'");
document.feedbackform.email.focus();return false}
}}

pimpinphp

11:58 am on Apr 10, 2005 (gmt 0)

10+ Year Member



I would rewrite this using regular expressions. That way as you go all you would need to do is alter the expression any time you needed to change what criteria you were trying to reject. It would also make the overal script smaller. Hope that helps.

Acternaweb

1:48 pm on Apr 10, 2005 (gmt 0)

10+ Year Member



I am brand new to javascript, not sure what you mean by regular expressions. I used google but still unclear how to set it up.

Greven

2:04 pm on Apr 10, 2005 (gmt 0)

10+ Year Member



Regular expressions are a standard method for searching through a string with specific quantifiers ie. match on the word "dog" only if its preceeded by "cat ", but don't if its preceeded by "cat", etc. There are alot of good tutorials on regular expressions, its well work learning.

Acternaweb

9:57 pm on Apr 10, 2005 (gmt 0)

10+ Year Member



I found one and made some good headway (I think), but am stuck. The "return false" doesn't work. If the email is correct, is does not "submit" as it is supposed to. Thanks for the help

emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}¦(com¦net¦org¦edu¦mil¦gov¦biz¦))$/
if (!emailRe.test(document.feedbackform.email.value))
alert(document.feedbackform.email.value + ' is invalid');
document.feedbackform.email.focus();return false;

}}

pimpinphp

2:01 am on Apr 11, 2005 (gmt 0)

10+ Year Member



This should work. You were close minor errors. Starting "{" after the if statement. When doing regex in javascript you need to tell it you are using regex by creating a regex object "var regex = new RegExp(emailRe);" and use that object to test off of your strings. Hope that helps. Here is my test script:

function foo(email){
var emailRe = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[.com¦.net¦.org¦.edu¦.mil¦.gov¦.biz¦]$";
var regex = new RegExp(emailRe);

if (!regex.test(email)) {
alert(regex.test(email));
return false;
}else{
document.write("ok");
}
}
foo("tesgffgt@sdfftest.biz")

Acternaweb

4:06 pm on Apr 11, 2005 (gmt 0)

10+ Year Member



Thanks but that didn't work. It allows for invalid email, then says "ok"