simcoweb Posted June 15, 2007 Share Posted June 15, 2007 This email field validation is showing the error message that the email is not valid. I can't see where/why it would decline to validate this email address: bozotheclown@hotmail.com Here's the code: // check to make sure email is valid format if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) { $errors[] = '<font color="red">Please provide a valid email address.</font>'; } Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted June 15, 2007 Share Posted June 15, 2007 This part... (\.[a-zA-Z0-9]+){1,2} ...should be: (\.[a-zA-Z0-9]+){1,4} In your regex the last part of the e-mail address - .com .net .org .dk .co.uk - has to be between 1 and 2 characters long, _including_ the . (dot). This means the tld of your domain name has to be 1 character long. No such tld's exists So I would recommend that you make it between 1 and 4 characters, since this will leave 3 characters to comprise the tld... this will make most tlds valid. ** I'll attend to this again in the morning if you haven't found a solution, but right now I'm too tired and what I've said above might not be 100% correct... but I'm sure it has something to do with your quantities... ** Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 16, 2007 Author Share Posted June 16, 2007 Hey, thanks for the post. And the description! It helps to learn it. Unfortunately it's still throwing an error even with that modification. I'm wondering if there's a way to have the error display exactly what is causing the issue so we can narrow down what segment of the regex is not being fulfilled. Anyone? Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted June 16, 2007 Share Posted June 16, 2007 To be honest with you, I don't use eregi()/ereg() - I use use preg_match() instead. You can easily find tutorials, articles, blogs ect. which claims preg_match() is faster and more powerful than ereg()/eregi() - I don't really know if that is true, but it is only good if it is. The reason why I'm telling you this is, that it has a slightly different syntax than ereg()/eregi()... its not a big deal. It almost comes down to adding slashes at the beginning and ending of your regex: "/^[a-z]+$/" instead of "^[a-z]+$". Here is my favorite e-mail regular expression - I wrote it my self but I'm sure it is possible to find identical regexes around the web, after all it's the best (with one exception which I'll get back to) <?PHP $email = $_POST['email]; //or something like that $regex = "/^([a-z0-9]+[a-z0-9\._-]*[a-z0-9]+)@([a-z0-9]+[a-z0-9-]*[a-z0-9]+)\.([a-z]{2,3})$/i"; if(preg_match($regex,$email)) { echo 'The e-mail address is valid; } else { echo 'The e-mail address is _not_ valid; } ?> But back to the regex: ^ ensures we start matching from the beginning at the string ([a-z0-9]+[a-z0-9_-]*[a-z0-9]+) one or more occurences of a-z 0-9 followed by any number of occurrences (maybe none) of a-z 0-9 . - _ followed by one or more occurrences of a-z 0-9 @ followed by a @ ([a-z0-9]+[a-z0-9-]*[a-z0-9]+) followed by one or more occurrences of a-z 0-9 followed by any number of occurrences (maybe non) of a-z 0-9 - followed by one or more occurrences of a-z 0-9 \.([a-z]{2,3}) followed by a . (dot) which is escaped with \ (backslash) because it's a special character followed by between 2 and 3 occurences of a-z (matching .dk .com .net ect.) $ ensures we match all the way to the end of the string /i makes the match incasesensetive... so instead of doing [a-zA-Z] all the time you can just do [a-z] and add /i at the end I mentioned that it was the best regex appart from one thing and thats the last part matching tlds (.com .net .dk ect.). Lots of domains wont match 2-3 letters... take .co.uk for example or .museum. Therefore I always do this: "/^([a-z0-9]+[a-z0-9\._-]*[a-z0-9]+)@([a-z0-9]+[a-z0-9-]*[a-z0-9]+)\.(dk|com|net|org|co\.uk)$/i"; This ensures that for example .co.uk can pass your validation. It has the downside that for example .lt or what ever wont because you haven't added it... but if you wanna be 100% sure you can make a list of all tlds in the world - it takes about 10minutes Unfortunately I'm in a hurry and haven't got time to explain what was wrong with your own regex, but maybe I'll get back to that later. Btw check this place out: http://www.regular-expressions.info/reference.html it's really good for regex references! Best regards Wuhtzu Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 16, 2007 Author Share Posted June 16, 2007 I found the issue. In the !eregi line I refer to the $_POST['email'] when it should be 'Email' (upper case E). So, no more eregi error but there's another issue that i'll have to post in the other forum. Thanks! Quote Link to comment Share on other sites More sharing options...
neel_basu Posted June 19, 2007 Share Posted June 19, 2007 You can use this regex to validate Emails. ^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$ case insensitive option Must be ON. e.g. Use i modifier. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.