preg_match issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ukfusion
    New Member
    • Sep 2007
    • 35

    preg_match issues

    Hi is there a specific way to check for preg_match.

    Only reason i ask is im having trouble even matching basics.

    I have a validation script with a function like this(for example...simpl ified for testing)

    Code:
    function AlphaNumeric($value){
    if(preg_match("/^a-z/", $value)){
    	return true;
    }
    }
    then i call it from a sepperate file such as

    Code:
    if(!$validation->AlphaNumeric($value){
    give error message...
    }
    But i always get an error...even when my value is just abc.

    what am i doing wrong here...have got a feeling im gonna feel stupid afterwards but please someone tell me what's going wrong.
    Last edited by Dormilich; Aug 31 '11, 06:31 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it’s your regular expression. you check, whether your string starts with the string "a-z". you certainly mean, whether your string starts with a letter, which is done through a character class.
    Code:
    public function alphabetic($value)
    {
        return (bool) preg_match("#^[[:alpha:]]#", $value);
        // or
    //  return (bool) preg_match("#^[a-z]#i", $value);
    }

    Comment

    • ukfusion
      New Member
      • Sep 2007
      • 35

      #3
      I thought [a-z] specified the range of letters between a and z? along with 0-9 etc or cant you use preg_match that way?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I thought [a-z] specified the range of letters between a and z?
        that’s correct, but you wrote
        Code:
        /^a-z/

        Comment

        Working...