Raised This Month: $51 Target: $400
 12% 

Just allow A-Z, a-z and 0-9 in String


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 02-12-2012 , 09:51   Just allow A-Z, a-z and 0-9 in String
Reply With Quote #1

Hey, I want to check if a String just contains the Letters from the Title.
I tried it with 2 for-loops but that doesnt work and I dont know why.

Here is my Code:

PHP Code:
public is_valid_username(szName[])
{
    new 
chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    
new validchar 0
    
    
for(new 0strlen(szName) - 1i++)
    {
        
validchar 0
        client_print
(0print_chatszName[i])
        for(new 
0strlen(chars) - 1j++)
        {
            if(
equal(szName[i], chars[j]))
            {
                
validchar 1
            
}
        }
        if(!
validchar)
        {
            return 
false
        
}
    }
    return 
true

would be great if you could help me.
have a nice day
__________________
Quote:
#define true ((rand() % 2)? true: false) //Happy debugging suckers

Last edited by mottzi; 02-12-2012 at 09:52.
mottzi is offline
Send a message via MSN to mottzi
RoaR
Member
Join Date: Dec 2011
Old 02-12-2012 , 10:06   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #2

I think plugin works but you did not define an action to be done when it doesn't equal that.

What did you want the plugin to do when user doesn't have those characters?
RoaR is offline
lucas_7_94
Leche Loco
Join Date: Mar 2009
Location: Argentina
Old 02-12-2012 , 10:25   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #3

I'm not sure if you should return inside loop , btw , try testing .

Code:
public is_valid_username(szName[]) {     new chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"         new bool:founded = false         for(new i = 0; i < strlen(szName) - 1; i++)     {         if( founded ) break;         client_print(0, print_chat, szName[i])         for(new j = 0; j < strlen(chars) - 1; j++)         {             if(equal(szName[i], chars[j]))             {                 founded = true             }         }     }     return founded }

I don't tested and i'm not sure if this works.
__________________
ATWWMH - MiniDuels
Madness is like gravity, just need a little push.

Last edited by lucas_7_94; 02-12-2012 at 10:25.
lucas_7_94 is offline
Send a message via Skype™ to lucas_7_94
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-12-2012 , 14:44   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #4

@Everyone
None of those will work. Also, you are overthinking.

Code:
public is_valid_username(szName[]) {     new len = strlen(szName);         for(new i = 0; i < len; i++)     {         if(!isalnum(szName[i]))         {             return false;         }     }         return true; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 02-12-2012 at 16:18. Reason: isalpha && isdigit -> isalnum
Exolent[jNr] is offline
killergirl
Senior Member
Join Date: Jul 2010
Old 02-12-2012 , 14:55   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #5

PHP Code:
#include <amxmisc>

public is_valid_username(szName[])
{
    new 
chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    
new validchar 0pos
    
    
for(new 0sizeof(chars) - 1i++){
    
pos containi(szNamechars[i])
    
    if(
pos == -1){
        
validchar 1
        
break;
    }
    }
    
    if(!
validchar)
        return 
true
    
else
        return 
false

killergirl is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-12-2012 , 14:57   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #6

Quote:
Originally Posted by killergirl View Post
PHP Code:
#include <amxmisc>

public is_valid_username(szName[])
{
    new 
chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    
new validchar 0pos
    
    
for(new 0sizeof(chars) - 1i++){
    
pos containi(szNamechars[i])
    
    if(
pos == -1){
        
validchar 1
        
break;
    }
    }
    
    if(!
validchar)
        return 
true
    
else
        return 
false

That won't work either.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 02-12-2012 , 16:00   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #7

What about regex?
__________________
xPaw is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-12-2012 , 16:18   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #8

Quote:
Originally Posted by xPaw View Post
What about regex?
I'm unsure of the efficiency comparisons for RegEx in AMXX, but I feel it would be overkill for such a small check.
If it were to be a more complicated pattern then for sure use RegEx.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 02-13-2012 , 00:59   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #9

Hey thank you exolent
didnt know there is existing a function like this

could you please explain, why my _solution_ didnt work?
__________________
Quote:
#define true ((rand() % 2)? true: false) //Happy debugging suckers

Last edited by mottzi; 02-13-2012 at 01:00.
mottzi is offline
Send a message via MSN to mottzi
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 02-13-2012 , 01:19   Re: Just allow A-Z, a-z and 0-9 in String
Reply With Quote #10

Quote:
Originally Posted by mottzi View Post
Hey thank you exolent
didnt know there is existing a function like this

could you please explain, why my _solution_ didnt work?
The logic you have in your function will return true if there is only one valid character.

The other major flaw is using equal() to compare two integers (ascii characters). You can't do that. You just need to compare them directly.
__________________

Last edited by fysiks; 02-13-2012 at 01:20.
fysiks is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 06:44.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode