Forum Moderators: coopster

Message Too Old, No Replies

checking valid email form - errors

         

surrealillusions

10:33 pm on Feb 9, 2008 (gmt 0)

10+ Year Member



Hi, i use php to check the contents of a form has been filled correctly. Only problem is that the email validation is a little too strict..it doesn't let anything through.

Heres the line that checks the field contents for the correct characters and format. But it always says the email is not valid. Strange thing is, it worked the other night when i first uploaded it. Went to add a captcha system to the form..but found out the email didn't work..even with no captcha in as before.

if(!preg_match('/^([a-zA-Z0-9\-\_\.]+\@[a-zA-Z0-9\-\_\.]+\.[a-zA-Z0-9\-\_])$/',$_POST['email'])){

Is there a mistake in this line somewhere? I use dreamweaver code view with highlighting, and the code colours look normal and not all one colour like you get when you leave a " or ' open or something..

:)

darrenG

2:36 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



Ive not read a huge lot into your pattern there (what can I say, its my day off!), but I believe that pattern will match any string which is NOT like an email address, ie an invalid email.

Does this tie in with your scripted logic? i.e., is that if statement catching valid or invalid email addresses? It could be as simple as removing the ^ from the pattern, or removing the! from the if condition.

surrealillusions

5:54 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



yeah..basically its only supposed to allow email formats thru and anything like gh@dfh.dfg@.adfg.dfj@ wont get through.

But even a valid email (one that i own) wont get through.

And scripted logic? I dont have much 'normal' logic let alone scripted logic ;)

:)

surrealillusions

6:55 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



Tried removing the ^ and!, one or the other and both, but no affect. no emails getting thru.

I then tried this line, a more basic approach
if(!preg_match('/^([a-zA-Z0-9\s]{3,50})$/',$_POST['email'])){

And it worked fine (except without the @ or dots). Just a random bunch of letters.

But i cant see anything wrong with the first line in the first post that can possibly be causing it to stop any email typed into the form...

darrenG

7:31 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



I've had a play with your pattern, try this:

'[a-zA-Z0-9\.\-\_]+@[a-zA-Z0-9\.\-]+\.[a-zA-Z]+'

surrealillusions

7:43 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



That pattern didn't work. I got some errors. However, i found what was causing it and this line now works

if(!preg_match('([a-zA-Z0-9\.\-\_]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z])' ,$_POST['email'])){

:)