Announcement

Collapse
No announcement yet.

Need help with if(ereg statement please

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with if(ereg statement please

    Hello! I hope someone can help a newbie here with one or more of these expression validation questions.

    I am starting out with a basic check like this,

    if(ereg("[^a-zA-Z]", $string)){

    [some error message]

    } else {

    [$string passes validation....]

    }

    This will validate and pass strings only with upper and lower case letters. I want to be able to incorporate the following:

    1) Allow a single instance of a hyphen -
    2) Allow any instances of 6 digit length numbers

    Kind regards

    Julie

  • #2
    Try the following:

    PHP Code:
    <?php

    $strings 
    = array(
        
    'test',
        
    '123456',
        
    '12345',
        
    '--',
        
    '-',
        
    '1234567',
        
    'abcdef-abc-123456-abcdef-',
    );

    foreach (
    $strings AS $string)
    {
        if (
    preg_match('
        /
        [^a-z-\d]       # anything, thats no letter, hyphen or digit
        |               # or
        -{2,}           # 2 or more hyphens in a row
        |               # or
        \d{7,}          # 7 or more digits in a row
        |               # or
            (?:^|[^\d]) # the beginning of the string or a non-digit
            \d{1,5}     # followed by 5 or less digits
            (?:[^\d]|$) # followed by the end of the string or a non-digit
        /ix'
    $string))
        {
            
    // display an error message
            
    echo "'$string' doesn't work\n";
        }
        else
        {
            
    // $string passes validation
            
    echo "'$string' works\n";
        }
    }
    ?>
    ... third attempt. Looks good so far
    Feel free to add more testcases.
    Last edited by Stadler; Sun 27 Feb '05, 4:31am.
    Hints & Tips:
    [[vB3] More Spiders / Indexers / Archives for vB3 - list]|[List of one-time-emails to ban]


    http://sfx-images.mozilla.org/affili...efox_80x15.png

    Comment

    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
    Working...
    X