View Full Version : validate form field
Nightfire
10-26-2002, 02:03 AM
I'm needing a bit of help with regexp. I dismantled this code
if(eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$", $email)){
to
if(eregi("^[a-z0-9]", $username)){
What I'm wanting is for a user to use only numbers or letters. If I use something other than those at the start, I get a message about it being an invalid character, which is what it's meant to do. But if I enter 10"ds! I get no error message. What do I need to do to make it search all the textbox for any illegal characters?
The ^ character means 'at the start of the string' - ergo lose that.
Also note: If you wanted to assure their username was between 5 and 12 letters/numbers long, you could use
"[A-Za-z0-9]{5,12}"
I think :) (ie. my regexes are pretty naff too)
Nightfire
10-26-2002, 11:12 AM
Thanks :)
I figured the length thing out when I was doing the email one :)
Nightfire
10-26-2002, 11:26 AM
I got it working. Incase anyone else needs it:
function username_valid($username) {
if(eregi("[a-zA-Z0-9]$", $username)){
return TRUE;
}else{
return FALSE;
}
}
Only seemed to work with the $ on the end. Anyone know what that's for?
I think it determines a proper end of an expression. Useful for OR clauses - eg [regexOne]$|[regexTwo]$
Maybe a regex guru will happen by and confirm that :)
btw - a good resource of precompiled regexes is regxlib . com (http://www.regxlib.com/)
mordred
10-26-2002, 12:55 PM
Indeed, validating user input with the help of regular expressions can get tricky. Here's an example that should work for you:
function username_valid($username) {
if (preg_match("/^[a-z0-9]+$/i", $username)) {
return true;
} else {
return false;
}
}
How does it work?
When you construct a pattern with regular expressions, think of how you would describe that pattern in spoken english. You might say sth. like "It should only consist entirely of letters". It's easy for a machine to get a definition of letters, but how should you translate "consist entirely"? Solution: A string consists entirely of letters if each character, from the start until the end of the string is a letter. That's something your regexp machine can understand. To sum it up, '^' means "at the start" and '$' means 'at the end'. Those symbols are also called anchors, because they kind of anchor a pattern at a certain position of the search string.
So let's dissect the pattern I used in this post:
/^[a-z0-9]+$/i
These slahses are delimiters, i.e. they tell the regexp machine that everything enclosed by them is considered a pattern.
/^[a-z0-9]+$/i
That's a modifier that is applied to everything inside the pattern. The "i" stands for "case-insensitive", so it achieves the same effect as eregi().
/^[a-z0-9]+$/i
In our character class, we put the character ranges a-z and o-9. Because of the "i" modifiere, we don't have to specify A-Z, since our matching will be case-insensitive. The class specifies the valid characters we want to match against $username.
/^[a-z0-9]+$/i
The "+" is a so-called quantifier. It describes how often the the sub-pattern left of the "+" may appear in the search string, and this case it's at least one or more times that characters of our character class may be in $username.
/^[a-z0-9]+$/i
Okay, we're almost finished. "foo123" would be matched by our pattern, but also "....foo123" if we didn't use ^ and $. Why? Because the regexp looks if it can find the pattern [a-z0-9]+ somewhere in $username, and that's the case for "...foo123". We need to say that it should start at the first character (^) and look until the last character ($).
Voila, that's all that is behind regular expressions. Well, there's surely more, but try to modify the patterns slightly and see what's matched and what not to get a better grip of them.
hth
Nightfire
10-26-2002, 02:52 PM
Wow :D Thanks for that. Makes it clearer.
ntthuan32
07-10-2008, 05:03 AM
Thanks a lot =D>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.