Hello, I need to validate a username which can only be alphanumeric and maybe contain an underscore.

I wrote this:

function validate_username($v_username) {
return (eregi("[a-zA-Z0-9]",$v_username));
}

which does not validate "#$%" but does validate "Ann#$%"

Can somebody help me and tell me which modfier I am supposed to add where to make the username accept only alphanumeric characters and underscores?

Thansk a lot.

    function validate_username($v_username) {
       return eregi('[^a-z0-9_]', $v_username) ? 'Invalid' : 'Valid';
    }
    

    True = Invalid in this.

      Is there any way to have it written so that it is valid if it is true so that I don't have to change the rest of my code where I have already called this function?
      Thanks

        function validate_username($v_username) {
           return eregi('[^a-z0-9_]', $v_username) ? FALSE : TRUE;
        } 
          5 days later

          I don't see any changes in what you just posted from what you posted before... I am trying to find out how to match a-z A-Z 0-9 and the underscore so that if it matches then it is true, if it doesn't, it is false

            Maybe I am crazy but I see the same pattern: [a-z0-9_]

            I am looking to have a function that returns true if the pattern is matched, this one returns false if the pattern is matched, or am I wrong?

              It does. He's using the ternary operator.

              IF the username contains any characters that are not [a-zA-Z0-9_], then it returns a FALSE (i.e. Invalid), otherwise TRUE (i.e. valid).

              If you want it written out:

              function validate_username($v_username) {
                 return eregi('[^a-z0-9_]', $v_username) ? FALSE : TRUE;
              }
              // That is the same as this:
              function validate_username($v_username){
                 if(eregi('[^a-zA-Z0-9_]', $v_username))
                 {
                    // THe username contains an inavlid character
                    return FALSE;
                 }
                 else
                 {
                    return TRUE;
                 }
              }

              ~Brett

                Whoa... wait a sec, isn't this flipped?

                function validate_username($v_username) { 
                   return eregi('[^a-z0-9_]', $v_username) ? FALSE : TRUE; 
                } 

                Shouldn't it be 'TRUE : FALSE', i.e. the part before the colon in a ternary op is executed if the condition is true, the part after the colon is used if the condition results to FALSE?

                  Thanks for the explanation...
                  I was aware of the ternary operator, but I guess I was reading it wrong and thought that if it DOES match that pattern it'd be false... I guess the ^ negates the whole thing turing it around.
                  Thanks again.

                    Yes. If you read up on RegEx (which I'm no expert) it tells you that if the carrot "" character is in a span field "[]", then it means NOT.

                    ~Brett

                      Write a Reply...