vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   How do you check for more than 1 letter for a variable? (https://vborg.vbsupport.ru/showthread.php?t=242888)

Boofo 05-19-2010 02:53 PM

How do you check for more than 1 letter for a variable?
 
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:

Code:

$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

Quote:

Originally Posted by Boofo (Post 2039898)
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:

Code:

$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

PHP Code:

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:

Code:

        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:

Code:

        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.

PHP Code:

    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.

PHP Code:

$errors = array ( );
if ( ... )
{
    
$errors[] = 'Error One';
}
if ( ... )
{
    
$errors[] = 'Error Two';
}
if ( ... )
{
    
$errors[] = 'Error Three';
}
if ( 
count $errors ) > )
{
    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.


All times are GMT. The time now is 08:00 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01224 seconds
  • Memory Usage 1,745KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_code_printable
  • (3)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete