Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-19-2010, 02:53 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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.
Reply With Quote
  #2  
Old 05-19-2010, 06:17 PM
Dylanblitz Dylanblitz is offline
 
Join Date: Oct 2005
Location: OC, California
Posts: 732
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Boofo View Post
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
Reply With Quote
  #3  
Old 05-19-2010, 06:56 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #4  
Old 05-19-2010, 09:14 PM
Dylanblitz Dylanblitz is offline
 
Join Date: Oct 2005
Location: OC, California
Posts: 732
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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');
    } 
Reply With Quote
  #5  
Old 05-20-2010, 06:17 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Couldn't you do elseif's there also?
Reply With Quote
  #6  
Old 05-20-2010, 08:19 PM
Dylanblitz Dylanblitz is offline
 
Join Date: Oct 2005
Location: OC, California
Posts: 732
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 05-20-2010, 08:35 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I've never done it with a variable like that. Can you give me an example how I could do that?
Reply With Quote
  #8  
Old 05-20-2010, 10:18 PM
Ryan Ashbrook's Avatar
Ryan Ashbrook Ryan Ashbrook is offline
 
Join Date: Dec 2002
Location: Cincinnati, Ohio
Posts: 422
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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).
Reply With Quote
  #9  
Old 05-20-2010, 11:48 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks. I'll play with that and see what I can come up with.
Reply With Quote
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. The time now is 05:59 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04369 seconds
  • Memory Usage 2,262KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (4)bbcode_code
  • (3)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete