I'm making a basic validation class for a CMS I'm developing. The following function verifies that a username contains nothing but letters, numbers and underscores. How can I extend this regexp to make sure that $value is no less than 5 characters and no more than 20?

    function isUsername($value, $msg) {

    if(eregi("[^a-z0-9_]", $value)) {
		$this->errorlist[] = array("value" => $value, "msg" => $msg);
		return false;
	} else {
		return true;
	}

}

Also, I have considered using a method like they used in this tutorial at DevShed, in which I could pass a validation function the name of a variable, and have it use this internal class function to retreive the value:

    function _getValue($field)
        {
            global ${$field};
            return ${$field};
        }

This way I could fetch the value from within my validation class, and in the case of error I can return the name of the variable it err'd on. This works fine for normal variables, but when it comes to passing an associative array (say $dirtydata["username"]) it cannot retreive the variable name. Is there a way to make this function find return the correct variable name, or is this only possible for a regular variable?

Thanks for any help in advance 🙂

    To make sure that $value is no less than 5 chars and no more than 20 try this.

    function isUsername($value, $msg) {

    if (strlen($value) < 5 || strlen($value) > 20) {
    	return false;
    }
    else {
    
        if(eregi("[^a-z0-9_]", $value)) {
    
            $this->errorlist[] = array("value" => $value, "msg" => $msg);
    
            return false;
    
        } else {
    
            return true;
    
        }
    
     }              

    }

      Thanks varasurf, although I should have specified that I was hoping for a way to extend the regexp to do the check for me (for the sake of simplicity and speed). If that isn't possible I will most likely use your approach.

        So nobody knows regexps?!?! Don't tell me I have to ask the Perl guys!

        j/k, thanks anyway... 😉

          if(preg_match("!^[a-z0-9_]{5,20}$!i", $value))
          {
            // $value is made up of required letters numbers and underscore
            // and ranges from 5 to 20 characters in length.
          }

          ! = delimiters.
          {min,max}
          i = modifier (insensitive).
          ^ start of string (line in multiline mode - negates when inside a character class).
          $ end of string (line in multiline mode).

            Thank you liljim, you're the best! I've read the the Perl compatible regexps are quicker than the standard ereg/eregi functions in PHP. Do you know if this is true, and if it's worth using them all the time?

            Anyway, thanks again!

              Hehe, np.

              To answer your question, yes they're faster (can't remember where I've seen some benches, you'll have to have a look around for some) and yes they're worth using over the ereg_ functions, not just for the speed of the replacements / matches, but for the flexibility in syntax. Read up on the pattern syntax and modifiers etc on the manual pages for more info. 🙂

                Write a Reply...