making sure a form entry contains only text and numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
suthie
Forum Commoner
Posts: 68
Joined: Sat Jun 09, 2007 10:46 am

making sure a form entry contains only text and numbers

Post by suthie »

How can I make it so that when a user is creating a username, php checks to make sure it is only letters and numbers and also limit the amount of characters in the entry?
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Check into regex and check ctype_alnum
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

For the length issue, you can use the maxlength="x" property of the textbox.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

ReverendDexter wrote:For the length issue, you can use the maxlength="x" property of the textbox.
But you will want to check with strlen as well, regex can also be setup to require a certain min and even a max number of characters. Reason for this is the HTML can be altered and the maxlength property can be changed.

Here is an example of regex:

Code: Select all

$regex1 = new regex('/^[a-zA-Z0-9]$/'); //Charecters a-z, A-Z, and 0-9 no limits
$regex1->test('test123TEST'); //true
$regex1->test('test 123'); //false has a space
$regex1->test('test!@#'); //false has non-alphanumeric characters

$regex2 = new regex('/^[a-zA-Z0-9]{3,}$/'); //Characters a-z, A-Z, and 0-9 must be at least 3 characters
$regex2->test('abc'); //true
$regex2->test('a'); //false has too few characters

$regex3 = new regex('/^[a-zA-Z0-9]{3,10}$/'); //Characters a-z, A-Z, and 0-9 must be between 3 and 10 characters
$regex3->test('dsfsd'); //true
$regex3->test('d'); //false again has too few characters
$regex3->test('dsfsdhfghghgfhfghghgf'); //false has more characters than allowed
*note I am using a regex class of mine. the function test(string) is a wrapper for preg_match. And you can use character classes with regex (ex. /w) to represent groups of characters (ex. /w = [a-zA-Z0-9]).
suthie
Forum Commoner
Posts: 68
Joined: Sat Jun 09, 2007 10:46 am

Post by suthie »

perfect!

this code works great:

Code: Select all

if (!ctype_alnum($username)){
         error('Usernames must consists of only letters and numbers..\\n'.
             'Please create a username that contains only letters and numbers.');
}
(error is a funciton defined earlier in the whole thing)

how can I make a minimum number of characters?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

suthie
Forum Commoner
Posts: 68
Joined: Sat Jun 09, 2007 10:46 am

Post by suthie »

perfect! thanks!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

// min len 5, max len 25
$is_ok = (preg_match('#^[a-z\d]{5,25}$#i', $string)) ? true : false;
Post Reply