NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP invalid characters

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 09-16-2005, 01:12 PM THREAD STARTER               #1 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




PHP invalid characters


Just a quicko, how can I define invalid characters for a user's input so that when a user registers to my site, they don't select a username such as:

"#~@L@N J0N3S~#"

lol, excuse the example. I only really want letters, numbers and hyphens to be used. Thanks
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 09-16-2005, 01:17 PM   #2 (permalink)
Account Closed
 
axilant's Avatar
Join Date: May 2004
Location: /etc/passwd
Posts: 2,178
axilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to behold
 

Member of the Month
July 2005

http://us2.php.net/ctype_alpha

I use these functions, but i have a class that i use them, but return false if its empty. I will think about posting it or not
????: NamePros.com http://www.namepros.com/programming/124238-php-invalid-characters.html

Here it is... heh

PHP Code:
function is_alpha($str ''//alpha
    
{
        if(
$str == "")
        {
            return 
false//string empty
        
}
        elseif(
ctype_alpha($str) == true)
        {
            return 
true;
        }
        else
        {
            return 
false//error, there must be a character than is not a letter!
        
}
    }
    function 
is_num($str ''//digits
    
{
        if(
$str == "")
        {
            return 
false//string empty!
        
}
        elseif(
ctype_digit($str) == true)
        {
            return 
true//its a non-decimal number! (base 10)
        
}
        else
        {
            return 
false//error, there must be a character than is not a digit!
        
}
    }
    function 
is_alnum($str ''//alphanumeric
    
{
        if(
$str == "")
        {
            return 
false//string empty
        
}
        elseif(
ctype_alnum($str) == true)
        {
            return 
true//this is 0-9, a-z!
        
}
        else
        {
            return 
false//error, there must be a character that is not a digit or letter!
        
}
    } 
Well some of it... i commented it pretty well, if you have any problems just ask.
Last edited by axilant; 09-16-2005 at 01:28 PM.
axilant is offline  
Old 09-16-2005, 01:41 PM THREAD STARTER               #3 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Thanks very much for the help
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 09-16-2005, 04:36 PM   #4 (permalink)
NamePros Regular
 
moondog's Avatar
Join Date: Jun 2004
Posts: 587
moondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to allmoondog is a name known to all
 



You should also be able to do a POSIX match.

Something like this should work too:

PHP Code:
$string $_POST[string_to_test];
if(
ereg('[^@.[:alnum:]]',$string)) {
????: NamePros.com http://www.namepros.com/showthread.php?t=124238
  
# code that catches invalid charactes here

In this example, the character class is negated with the beginning '^', and you would put any *VALID* characters in the class. In this case, you would allow the "@", a ".", and any alphanumbers (A-Z, a-z, 0-9)

-Bob
__________________
Can't wait to be out of this forsaken business. Getting close! :)
moondog is offline  
Old 09-17-2005, 05:10 AM THREAD STARTER               #5 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Thanks Bob, I am currently using

PHP Code:
if (!ctype_alnum($username)) {
       
      echo 
'<script>alert("The username must consist of only alphanumeric characters (a-z 0-9).");</script>';
echo 
'<script>history.back(1);</script>';
exit;
   } 
Is this wise?
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 09-17-2005, 09:57 AM   #6 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,552
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
I do like Bob suggested. A regular expression is fine and more flexible IMO.

For example I use code like this to test a user name, that means only letters and figures are allowed and the string must be between 4 and 16 chars long. It does the job nicely
PHP Code:
if (!eregi("^([a-zA-Z0-9]{4,16})$"$username )) {
????: NamePros.com http://www.namepros.com/showthread.php?t=124238
echo 
"Error: the user name is not valid blah..."
sdsinc is offline  
Old 09-17-2005, 12:00 PM THREAD STARTER               #7 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




sdsinc, I just changed my code to the code you posted. But how do I make it case sensitive?
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 09-17-2005, 12:02 PM   #8 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,552
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
Well you just change eregi to ereg
sdsinc is offline  
Old 09-17-2005, 12:07 PM THREAD STARTER               #9 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Ah, simple as that eh? Thanks a lot
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 09-17-2005, 08:46 PM   #10 (permalink)
A Wealth of Knowledge
 
stscac's Avatar
Join Date: Aug 2004
Posts: 3,809
stscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud of
 



I use eregi a lot when validating email addresses and other forms of data - very useful.

If you want to see other ways it can be used, check out the PHP Manual documentation
http://us2.php.net/eregi

-Steve
stscac is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Getting started with PHP (The Basics) deadserious Webmaster Tutorials 60 11-17-2007 12:35 PM
Googlism - What does google think of you? deadserious The Break Room 55 12-15-2005 10:09 AM
Seeking php function to strip invalid characters from string RJ Programming 9 04-01-2005 12:34 AM

Liquid Web Smart Servers  
All times are GMT -7. The time now is 12:28 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger