Portal Home > Knowledgebase > Articles Database > PHP input validation (eregi)


PHP input validation (eregi)




Posted by Looie, 03-19-2008, 04:22 PM
Can someone please explain eregi()? I know (well, I think so - it works anyway) that for numbers only it's [0-9] and for letters it's [a-z], but what if I want either numbers or letters, or both (i.e. for a username or something)? Also, how would I make sure it's a valid email, and how to make sure it's a valid domain. I've looked on Google and php.net but I found it quite complicated One more thing, is eregi even right for this (validating input data), or is there a better function? Thanks

Posted by etogre, 03-19-2008, 04:53 PM
Here is my personal validator: validator.class.php Here is a very, very simple procedural implementation of this code test.php --- -- THE WATERED DOWN VERSION THAT YOU PROBABLY CARE ABOUT the function you are looking for is: Output: Use preg_match, it's supposedly faster and it's what everyone uses. Sorry about the little lesson... also understanding how to match regex's is a ballpark of its own. I use that email regex and have yet to have an issue with it. Also one thing to note about regex's is limiting them TOO MUCH is a bad thing, as you will sometimes end up with wierd cases (such as somebody@musedoma.museum) (which is an actual TLD). The regex I use there is pretty flexible, but with that said emails like "me@myfake.tld" will work as well, but generally that isn't a big issue. Last edited by etogre; 03-19-2008 at 05:03 PM.

Posted by etogre, 03-19-2008, 07:00 PM
Getting all ternary on it

Posted by Looie, 03-20-2008, 11:09 AM
Thanks for the explanation, but I still don't understand what this means =\

Posted by etogre, 03-20-2008, 12:06 PM
It's not so much understanding what it means, it's understanding that it works It's a regular expression, that searches the string to make sure it has characters a-Z-0-9 and . -, then it makes sure it has a @, and then finally that it has a . somewhere in the last part. Regular expressions (regexes) are like a programming language of their own almost.

Posted by nnormal, 03-20-2008, 04:16 PM
http://www.php.net/manual/en/referen...ern.syntax.php

Posted by Looie, 03-20-2008, 07:01 PM
Would this be okay for a username/password? (Would it just allow a-z, A-Z and 0-9?) ?

Posted by etogre, 03-20-2008, 07:35 PM
bad`u-ser'name" returns false "goodusername05" returns true

Posted by Saeven, 03-20-2008, 09:48 PM
That may get tedious though, as you are assuming that your users really want to retype usernames until they get it right. To avoid that kind of attrition (possible!), I'd recommend cleaning it up to your satisfaction and telling the user that the username used differs from the one they typed. Just a $0.02 Alex



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Cloud Setup (Views: 526)
Ip Problem (Views: 503)
DNS and GOdaddy (Views: 506)