PDA

View Full Version : very basic php input validation q?


mat
11-17-2002, 10:37 AM
checking for '@' symbol...

<?php

if (ereg("@",$email)){
echo"you entered a vaild email";
}else{
echo"Invalid!";
}

?>


How can i get it to check for the '.' symbol as well?


mat,

Nightfire
11-17-2002, 09:09 PM
if(eregi("^[a-zA-Z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$", $email)){
echo 'Valid email';
}else{
echo 'Invalid email';
}


Checks for letters and numbers, dashes and dots, etc.

mat
11-17-2002, 09:25 PM
:thumbsup: cheers!, works well.

whammy
11-18-2002, 04:35 AM
P.S. Unbeknownst to most web developers, + and ' are allowed before the @ sign for some email hosts, and ' after the @ sign as well... so:

"^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z]{2,}$"

is a good basis to work with, as far as I have experienced. (So far, so good, anyway) :)

Otherwise you'll get a very few ticked off customers saying something like:

"Why doesn't "John.O'Reilly+Management@O'Reilly.com" work?

Although as far as I know, a single quote STILL isn't valid in a URL... but some clients insist on that after the @ for some reason (anyone have any clarification on that?).

:D

Ökii
11-18-2002, 08:42 AM
or

/^([a-zA-Z0-9_\+'\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\+'-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i

would also validate IP based mail addresses.

firepages
11-18-2002, 10:44 AM
Originally posted by ÖĞii
/^([a-zA-Z0-9_\+'\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\+'-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i


.... thats easy for you to say ;)

Ökii
11-18-2002, 05:15 PM
hehe

not overly sure if it works perfectly (or at all for that matter) as I nabbed it from an asp regex site and had to do a couple of amendments myself. Someone shout yay or nay if they bother testing it.

btw - regex site = http://www.regxlib.com/

whammy
11-20-2002, 12:00 AM
Actually I have tried a few of those (and jkd's), and most of them I have tried actually get TOO strict, and don't allow some perfectly legal email addresses. That's why I just stick with a somewhat strict, but also loose one as I posted. :D

One thing about it, it's been in use for months on an intranet customer service site where they get hundreds and hundreds of emails a day, so whenever I find out ANYTHING is wrong with it (because a Customer Service rep says "Hey your script isn't working!") I update the regular expression. And I haven't heard anything in quite a while... :)

And I can tell you right now the one Nightfire posted to begin with looks similar to the one I started with - and as you can see it will immediately reject some perfectly valid email addresses; however, it's also incorrect since the "." does not have a backslash in front of it, which will allow absolutely ANYTHING in that section! :D