[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 09-16-2005, 12:12 PM   #1 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


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
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 09-16-2005, 12:17 PM   #2 (permalink)
Account Closed
 
axilant's Avatar
 
Join Date: May 2004
Location: /etc/passwd
Posts: 2,194
0.00 NP$ (Donate)

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


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

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 12:28 PM.
axilant is offline  
Old 09-16-2005, 12:41 PM   #3 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


Thanks very much for the help
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 09-16-2005, 03:36 PM   #4 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


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)) {
  
# 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
__________________
The mass purge has begun.
moondog is offline  
Old 09-17-2005, 04:10 AM   #5 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


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?
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 09-17-2005, 08:57 AM   #6 (permalink)
Domains my Dominion
 
sdsinc's Avatar
 
Join Date: Aug 2005
Location: Web 1.0
Posts: 6,284
1,095.94 NP$ (Donate)

sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute

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
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 )) {
echo
"Error: the user name is not valid blah...";
sdsinc is offline  
Old 09-17-2005, 11:00 AM   #7 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


sdsinc, I just changed my code to the code you posted. But how do I make it case sensitive?
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 09-17-2005, 11:02 AM   #8 (permalink)
Domains my Dominion
 
sdsinc's Avatar
 
Join Date: Aug 2005
Location: Web 1.0
Posts: 6,284
1,095.94 NP$ (Donate)

sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute

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
Well you just change eregi to ereg
sdsinc is offline  
Old 09-17-2005, 11:07 AM   #9 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


Ah, simple as that eh? Thanks a lot
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 09-17-2005, 07:46 PM   #10 (permalink)
A Wealth of Knowledge
 
stscac's Avatar
 
Join Date: Aug 2004
Posts: 3,794
47.60 NP$ (Donate)

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

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
Tutorial: Getting started with PHP (The Basics) deadserious Webmaster Tutorials 60 11-17-2007 11:35 AM
Googlism - What does google think of you? deadserious The Break Room 55 12-15-2005 09:09 AM
Seeking php function to strip invalid characters from string RJ Programming 9 03-31-2005 11:34 PM

Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 12:21 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85