PDA

View Full Version : How do you check for more than 1 letter for a variable?


Boofo
05-19-2010, 02:53 PM
How do you check to make sure that a variable only allows 1 letter or number? Here is the variable I am using in the php file:

$vbulletin->GPC['letter']



I want to do a check to make sure only one letter or number is entered. Right now you can enter anything for it.

Dylanblitz
05-19-2010, 06:17 PM
How do you check to make sure that a variable only allows 1 letter or number? Here is the variable I am using in the php file:

$vbulletin->GPC['letter']



I want to do a check to make sure only one letter or number is entered. Right now you can enter anything for it.

Untested but it should work


if ((strlen($vbulletin->GPC['letter']) == 1) && preg_match('/^[a-zA-Z0-9]+$/', $vbulletin->GPC['letter']))
{
//good value processing
} else {
//error code here
}


First part makes sure it's only 1 character long, second part makes sure it's only letters and numbers

Boofo
05-19-2010, 06:56 PM
You are the man! Thank you!

I was way off on the regex as I suck at those. Here is what I ended up using:

if (!(strlen($vbulletin->GPC['letter']) == 1) && preg_match('/^[a-zA-Z0-9]+$/', $vbulletin->GPC['letter']))
{
print_stop_message('cannot_have_null_values');
}


I will be using a different stop message, but I wanted to make sure I could get it to work first. I have another condition before this one that checks to make sure all 3 variables (letter + 2 others) are not blank. I was going to add this to that but I wanted to catch the letter variable separately. This is how it all looks together for the error checking:

if (!$vbulletin->GPC['quote'] OR !$vbulletin->GPC['name'] OR !$vbulletin->GPC['letter'])
{
print_stop_message('cannot_have_null_values');
}

if (!(strlen($vbulletin->GPC['letter']) == 1) && preg_match('/^[a-zA-Z0-9]+$/', $vbulletin->GPC['letter']))
{
print_stop_message('cannot_have_null_values');
}


If you have any suggestion s or advice, I am all ears. And thank you again. ;)

Dylanblitz
05-19-2010, 09:14 PM
If you are going to split it up and give messages I'd do something like this and split it a bit more, otherwise the user might get confused with one message for multiple conditions.

if (!$vbulletin->GPC['quote'] OR !$vbulletin->GPC['name'] OR !$vbulletin->GPC['letter'])
{
print_stop_message('cannot_have_null_values');
}

if (strlen($vbulletin->GPC['letter']) != 1)
{
print_stop_message('only_1_character_allowed');
}

if (!preg_match('/^[a-zA-Z0-9]+$/', $vbulletin->GPC['letter'])
{
print_stop_message('only_numbers_letters_allowed') ;
}

Boofo
05-20-2010, 06:17 PM
Couldn't you do elseif's there also?

Dylanblitz
05-20-2010, 08:19 PM
You could use an elseif if you want. I normally would do it differently. I would usually add the errors to a variable then output the variable, that way all the errors are shown at once instead of the user having to submit 3 times before he see's all the errors.

Boofo
05-20-2010, 08:35 PM
I've never done it with a variable like that. Can you give me an example how I could do that?

Ryan Ashbrook
05-20-2010, 10:18 PM
Something like this should suffice.


$errors = array ( );
if ( ... )
{
$errors[] = 'Error One';
}
if ( ... )
{
$errors[] = 'Error Two';
}
if ( ... )
{
$errors[] = 'Error Three';
}
if ( count ( $errors ) > 0 )
{
echo ( '<div>The following errors occurred.</div><ol>' );
foreach ( $errors AS $error )
{
echo ( '<li>' . $error . '</li>' );
}
echo ( '</ol>' );
die ( );
}

Modify to your needs, of course, this is just a basic example (that I personally use). :)

Boofo
05-20-2010, 11:48 PM
Thanks. I'll play with that and see what I can come up with.