most concise way to validate a string

Discussion in 'PHP' started by mnymkr, Sep 26, 2007.

  1. #1
    I was wondering what the most concise way to see

    if this string is not null or not 8 characters long or not numbers do this

    I am just not sure how to put all of those conditions together
     
    mnymkr, Sep 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Regular expression.

    
    
    if (!preg_match('/^\d{8}$/', $string))
    {
        // Invalid string
    }
    
    PHP:
     
    nico_swd, Sep 26, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    that seems to be the talk of the day these days.....

    would that also check for spaces

    for instance i was gonna use

    strlen to check for how many characters


    say it was an 8 number string

    12345678

    if i use strlen then they could still enter

    12345 78 and it would pass
     
    mnymkr, Sep 26, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    strlen() returns the length of the string. A space is counted as character, so 12345 78 would be an 8 character long string.
     
    nico_swd, Sep 26, 2007 IP
  5. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #5
    yes , but preg match woudln't do this?
     
    mnymkr, Sep 26, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    With the right pattern, it won't. I didn't test my code above my I'm pretty confident that it works like it's supposed to.
     
    nico_swd, Sep 26, 2007 IP
  7. xemiterx

    xemiterx Peon

    Messages:
    62
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    nico_swd's regular expression answer fits your requirement of "most concise" in the original post.

    If you are looking for a non regular expression answer you can convert the string to an integer and then if it is less than 10000000 or more than 99999999 it is invalid. Otherwise, valid.

    <?PHP
    
    $string = '12345678';
    $number = intval($string);
    
    echo "$string => $number => "; 
    
    if ($number < 10000000 || $number > 99999999) {
    	echo 'invalid';
    } else {
    	echo 'valid';
    }
    
    ?>
    PHP:
     
    xemiterx, Sep 26, 2007 IP
  8. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #8
    is there an advantage/disadvantage to using a regular expression here
     
    mnymkr, Oct 15, 2007 IP
  9. Invent

    Invent Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Less code would be the obvious one.
     
    Invent, Oct 15, 2007 IP