SitePoint Sponsor |
|
User Tag List
Results 1 to 19 of 19
-
Apr 26, 2003, 06:22 #1
- Join Date
- Jan 2002
- Location
- Canada
- Posts
- 6,364
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Preg_match to confirm letters/numbers/underscores only?
Hey, I've had people register with ^, *, and allt hese other weird characters.
I want to confirm on registration that it's just letters, numbers, underscores, or dashes.
How can I do this?
Regards,
Someonewhois- Nathan
-
Apr 26, 2003, 06:42 #2
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this:
PHP Code:if((ereg("^[a-zA-Z0-9_\-]+$", $your_variable_here)) {
# allowable characters only
} else {
# some bad characters found
}
You may need to escape the underscore as well ? using /_ for example as I've not tested this.
-
Apr 26, 2003, 06:47 #3
- Join Date
- Feb 2003
- Posts
- 156
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think there is no longer any reason to use ereg, use preg instead.
In brackets [] every character is literal IIRC, so you should not try to escape the -, instead make sure it is the last character in the class (as it already is in your example). By adding the \ you are also allowing the backslash as a valid character.
-
Apr 26, 2003, 07:27 #4
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Wasn't sure as I don't allow the - character through my FORMS as this can be used to hack mySQL 8)
-
Apr 26, 2003, 07:31 #5
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by R. U. Serious
POSIX -> PCRE (Perl) Regex
I think this would serve: /[\w-]+/
(\w includes an underscore)
While you are at it, you could also use {6,12} style syntax instead of + to control the maximum length of the string.Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Apr 26, 2003, 07:32 #6
- Join Date
- Mar 2001
- Location
- Philadelphia, US
- Posts
- 2,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by samsm
TuitionFree — a free library for the self-taught
Anode Says... — Blogging For Your Pleasure
-
Apr 26, 2003, 07:39 #7
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
wouldn't this work?
PHP Code:preg_match('#^[A-Za-z0-9_-]{3,20}$#s', $string);
PHP Code:preg_match('#^[A-Za-z0-9?+*_!#$%&-]{6,20}$#s', $string);
- website
-
Apr 26, 2003, 07:43 #8
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
but would \w include any 'special' letters like we have here? áéðæþö ?
- website
-
Apr 26, 2003, 07:58 #9
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by anode
PHP Code:preg_match('#^[A-Za-z0-9_-]{3,20}$#s', $string);
\w is [0-9A-Za-z_] ... no funky characters :-)
Besides the underscore that was on my post for about 10 seconds, I did make another mistake ... failed to put the ^ and $ in so that it only matched if it was the entire string.Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Apr 26, 2003, 08:15 #10
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok, preg code verision 2
username:
PHP Code:preg_match('#^[\w-]{3,20}$#', $string);
PHP Code:preg_match('#^[\w?+*!#$%&-]{6,20}$#', $string);
but about the \w
Originally Posted by The Manual
is \w maybe just an 'alias' for a-zA-Z0-9_ ? as you said ?- website
-
Apr 26, 2003, 09:20 #11
- Join Date
- Sep 2001
- Location
- Singapore
- Posts
- 5,269
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Actually, áéðíóúýþæö and 0-9 are matched by \w.
-
Apr 26, 2003, 10:03 #12
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hmm, so that is not very 'internet friendly' to use \w, propably best to limit it to [a-zA-Z0-9_-] I think...
- website
-
Apr 26, 2003, 10:16 #13
- Join Date
- Sep 2001
- Location
- Singapore
- Posts
- 5,269
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by website
-
Apr 26, 2003, 10:17 #14
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by redemption
PHP Code:$source = 'áéðíóúýþæö';
if (preg_match('/\w+/', $source))
{
echo ('match!');
}
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Apr 26, 2003, 10:21 #15
- Join Date
- Sep 2001
- Location
- Singapore
- Posts
- 5,269
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Funny, because I'd tested this out to confirm too, with this code snippet:
PHP Code:$string = 'áéðíóúýþæö232';
if ( preg_match( '/^\w+$/', $string) ) {
echo 'Match!';
}
-
Apr 26, 2003, 10:25 #16
- Join Date
- Sep 2001
- Location
- Singapore
- Posts
- 5,269
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by PHP function reference user contributed notes
-
Apr 26, 2003, 10:41 #17
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by redemption
- website
-
Apr 26, 2003, 10:42 #18
- Join Date
- Nov 2001
- Location
- Atlanta, GA, USA
- Posts
- 5,011
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by redemption
Of course, if you are installing a script on a server with a locale that includes áéðíóúýþæ in \w, there is a good chance that you would want to accept those characters.Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
-
Apr 26, 2003, 10:43 #19
- Join Date
- Oct 2002
- Location
- Iceland
- Posts
- 1,238
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by redemption
- website
Bookmarks