Prevent special character in username

This is a discussion on "Prevent special character in username" within the PHP Forum section. This forum, and the thread "Prevent special character in username are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Sep 19th, 2007, 13:00
Reputable Member
Join Date: Sep 2007
Location: Tehran - Iran
Age: 29
Posts: 434
Blog Entries: 2
Thanks: 7
Thanked 7 Times in 7 Posts
Prevent special character in username

Hi
I want prevent users to enter a username with special characters like space when they are registering a new one.
How can i do it ?
Is it regular expression or something else ?
thnx
Last Blog Entry: Throughout IRAN (Dec 10th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Sep 19th, 2007, 13:27
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Prevent special character in username

Yes you need to use a regular expression. This one only allows letters and numbers and uses the variable $username which would be $_POST['username']

PHP: Select all

if(!ereg("^[A-Za-z0-9]{4,30}$",$username)) {
    
//do error code here

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Sep 19th, 2007, 13:49
Reputable Member
Join Date: Sep 2007
Location: Tehran - Iran
Age: 29
Posts: 434
Blog Entries: 2
Thanks: 7
Thanked 7 Times in 7 Posts
Re: Prevent special character in username

Thanx
Last Blog Entry: Throughout IRAN (Dec 10th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Sep 19th, 2007, 13:55
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Prevent special character in username

also you can set the amount of characters you are allowing between the curly braces.

In my example you are allowing between 4 and 30 charaters
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
input $_SESSION['username'] = $username; on login page help Aaron1988 PHP Forum 2 Jan 28th, 2008 14:14
Chinese Character Sheela Starting Out 10 May 24th, 2007 13:54
Creating a character name Throntel Flash & Multimedia Forum 2 Dec 3rd, 2006 00:09
Character Encoding Handling spinal007 Classic ASP 0 Jun 13th, 2006 11:21
prevent refresh in asp simonneaves Classic ASP 2 Oct 10th, 2005 09:04


All times are GMT. The time now is 16:28.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42