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

Reply
 
Thread Tools Display Modes
  #1  
Old 01-04-2006, 12:50 AM
Lionel Lionel is offline
 
Join Date: Dec 2001
Location: Delray Beach, Florida
Posts: 3,277
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Simple regex question

I am getting a $value from a $_POST['value'] from a textbox

How can I make sure that only "digits" and "spaces" are allowed, and nothing else?

eg 55 32 etc...

but no 55-32 or abcd or [ or * or etc...
Reply With Quote
  #2  
Old 01-04-2006, 12:20 PM
harmor19 harmor19 is offline
 
Join Date: Apr 2005
Posts: 1,324
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
if(!is_numeric($_POST['value']))
{
  die(
'Only numerical values');

I don't know if it'll allow spaces
Reply With Quote
  #3  
Old 01-04-2006, 03:14 PM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That will die on spaces. This should work.

PHP Code:
$test_one '1 23465';

if(
preg_match('#[^0-9\s]#',$test_one))
{
    
$result 'Ahah! Found an evil character.';
}
else
{
    
$result 'Only Digits and Spaces here!';
}

echo 
$result
You you need to check multiple items use it as a function.
PHP Code:
$test_one '1 23465';

if(
digitsOnly($test_one))
{
    
$result 'Ahah! Found an evil character.';
}
else
{
    
$result 'Only Digits and Spaces here!';
}

echo 
$result;

function 
digitsOnly($target)
{
    return 
preg_match('#[^0-9\s]#'$target);

Reply With Quote
  #4  
Old 01-04-2006, 03:42 PM
Lionel Lionel is offline
 
Join Date: Dec 2001
Location: Delray Beach, Florida
Posts: 3,277
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Both methods stop valid and invalid characters.
Reply With Quote
  #5  
Old 01-04-2006, 05:00 PM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Not according to your original post. Is there something else you were trying to block. The regex above should return false for digits and spaces only, true for anything else.
Reply With Quote
  #6  
Old 01-04-2006, 07:33 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
if (preg_match("/^[0-9\w]+$/siU"$subject))
{
    
// valid

or
PHP Code:
function validate($subject)
{
    for (
$i 0$i strlen($subject); $i++)
    {
        if (!
is_numeric($subject{$i}) or $subject{$i} != " ")
        {
            return 
false;
        }
    }
    return 
true;

Reply With Quote
  #7  
Old 01-04-2006, 09:04 PM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by filburt1
PHP Code:
if (preg_match("/^[0-9\w]+$/siU"$subject))
{
    
// valid

or
PHP Code:
function validate($subject)
{
    for (
$i 0$i strlen($subject); $i++)
    {
        if (!
is_numeric($subject{$i}) or $subject{$i} != " ")
        {
            return 
false;
        }
    }
    return 
true;

Not working for me.

The c wrapper ctype_digit() is much faster than is_numeric if your going to go that way about it.

Code:
^0-9\s //Anything that does not "^" have the value of a digit "0-9" or a space "\s".
^\d\s works also and might be a bit faster
Run this test on your server Lionel. It works for every combo I have tried.

PHP Code:
$test = array(
    
'55 32',
    
'123b4',
    
'4-55',
    
'9543',
    
'*/12-b',
    
'&f54gt',
    
'[1234]',
    
'12 3',
    
'end'
);

for(
$i 0$i 9$i++)
{
    echo 
digitsOnly($test[$i]) . '<br />';
}



function 
digitsOnly($target)
{
    if(
preg_match('#[^\d\s]#'$target))
    {
        return 
'Ahah! Found an evil character in <b>' $target '</b>';
    }
    else
    {
        return 
'Only Digits and Spaces here! in <b>' $target '</b>';
    }

Reply With Quote
  #8  
Old 01-06-2006, 11:58 PM
Lionel Lionel is offline
 
Join Date: Dec 2001
Location: Delray Beach, Florida
Posts: 3,277
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I finally got it to work by exploding the values in the array and extracting them individually.


PHP Code:
 if (!empty($_POST['predminutes1'])){
$count1 0;
foreach(
$_POST['predminutes1'] as $mkey1=>$gvalue1) {
$gsub1 explode(' '$gvalue1);
foreach(
$gsub1 as $gnumber1) {
     
# check value to add to count
     
if(!empty($gnumber1))
        
$count1++; 
if (
$count1 $predscore1) {
    eval(
print_standard_error('error_invalidscorers'));
}
if (!
preg_match('/^([0-9]*)$/'$gnumber1)){
    eval(
print_standard_error('error_invalidcharacters'));
}
}
}

Reply With Quote
  #9  
Old 01-07-2006, 01:25 AM
akanevsky akanevsky is offline
 
Join Date: Apr 2005
Posts: 3,972
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
if (preg_match("#^[\d\s]*$#"$subject))
{
    
// valid

Reply With Quote
  #10  
Old 01-07-2006, 01:31 AM
Code Monkey's Avatar
Code Monkey Code Monkey is offline
 
Join Date: May 2004
Posts: 1,080
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lionel
I finally got it to work by exploding the values in the array and extracting them individually.


PHP Code:
 if (!empty($_POST['predminutes1'])){
$count1 0;
foreach(
$_POST['predminutes1'] as $mkey1=>$gvalue1) {
$gsub1 explode(' '$gvalue1);
foreach(
$gsub1 as $gnumber1) {
     
# check value to add to count
     
if(!empty($gnumber1))
        
$count1++; 
if (
$count1 $predscore1) {
    eval(
print_standard_error('error_invalidscorers'));
}
if (!
preg_match('/^([0-9]*)$/'$gnumber1)){
    eval(
print_standard_error('error_invalidcharacters'));
}
}
}

That's a lot of code to do a simple task.
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 10:29 AM.


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.04988 seconds
  • Memory Usage 2,296KB
  • Queries Executed 11 (?)
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
  • (1)bbcode_code
  • (11)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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_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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete