Checking for invalid symbols? (PHP) -
.Mento - 01.06.2013
I'm making a register option on my website but I can't figure out how to detect the invalid sa-mp nickname symbols, I was wondering if anyone here has a snippet that allows the following:
alphabetical characters, all numbers 0 - 9 and these symbols: ( ) [ ] _ .
and which returns invalid when symbols like @!+$%^&* are found.
I didn't know where to post this other than this section, please move if it's supposed to be somewhere else.
Re: Checking for invalid symbols? (PHP) -
nielsbon1 - 01.06.2013
Well you could use:
Код:
$str = $_POST['username'];
return !preg_match('/[^A-Za-z0-9.#\\-$]/', $str);
Those check if the string contains invalid characters,
So all you have to do is to place that in your code and edit the characters that you do not want them to put in to their name. It will return true or false, ofcourse you can also put it in your IF statement.
Re: Checking for invalid symbols? (PHP) -
Kirollos - 01.06.2013
i made a function that checks if every character matches an array which contains valid symbols.
PHP код:
<?php
function IsNameValid($str)
{
$valid = array(
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'(',
')',
'_',
'[',
']',
'.',
);
$count = 0;
for($a = 0; $a < strlen($str); $a++)
{
for($b = 0; $b < sizeof($valid); $b++)
{
if($str[$a] == $valid[$b]) break;
$count ++;
}
if($count == sizeof($valid)) return 0;
else {
$count = 0;
continue;
}
}
return 1;
}
?>
then you can use
PHP код:
<?php
if(IsNameValid($_POST['thename']))
{
//pass
}
else
{
//invalid character found!
}
?>
EDIT: the guy above me made more simple one
Re: Checking for invalid symbols? (PHP) -
.Mento - 01.06.2013
Thanks alot both
Re: Checking for invalid symbols? (PHP) -
playbox12 - 01.06.2013
Quote:
Originally Posted by kirollos
i made a function that checks if every character matches an array which contains valid symbols.
PHP код:
<?php
function IsNameValid($str)
{
$valid = array(
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'(',
')',
'_',
'[',
']',
'.',
);
$count = 0;
for($a = 0; $a < strlen($str); $a++)
{
for($b = 0; $b < sizeof($valid); $b++)
{
if($str[$a] == $valid[$b]) break;
$count ++;
}
if($count == sizeof($valid)) return 0;
else {
$count = 0;
continue;
}
}
return 1;
}
?>
then you can use
PHP код:
<?php
if(IsNameValid($_POST['thename']))
{
//pass
}
else
{
//invalid character found!
}
?>
EDIT: the guy above me made more simple one
|
Why would you take the time to do that? You can use regular expressions straight from PHP.
Re: Checking for invalid symbols? (PHP) -
Kirollos - 01.06.2013
Quote:
Originally Posted by playbox12
Why would you take the time to do that? You can use regular expressions straight from PHP.
|
i just didn't know about regex..