4
\$\begingroup\$

I'm looking to improve this class - any suggestions? It checks for empty, name, email, and password. The regex for email is very simple. A very lengthy article from the Linux Journal for improving upon this is here. The character set for password I borrowed from the NASA signup page. For name I allow letters, periods, and dashes. Testing for empty was developed with in this SO Post

class check 
  {
  static function empty_user($a)
    {
    return (int)!in_array('',$a,TRUE); 
    }
  static function name($a)          
    {
    return preg_match('/^[a-zA-Z-\.]{1,40}$/',$a);
    }
  static function email($a)
    {
    return preg_match('/^[a-zA-Z0-9._s-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{1,4}$/',$a);
    }
  static function pass($a)
    {
    return preg_match('/^[a-zA-Z0-9!@#$%^&*]{6,20}$/',$a);
    }
  }
\$\endgroup\$
7
  • 1
    \$\begingroup\$ You really shouldn't use a regex to validate email addresses. Also, why is this in a class? A namespace would make more sense. \$\endgroup\$
    – Maxpm
    Sep 20, 2011 at 2:54
  • \$\begingroup\$ For email validation/sanitization look at the built in (PHP 5.2+) filter functions. php.net/manual/en/filter.examples.sanitization.php \$\endgroup\$
    – user7212
    Sep 20, 2011 at 2:55
  • \$\begingroup\$ is it possible to see the regex they use for the filters? \$\endgroup\$
    – Chris Aaker
    Sep 20, 2011 at 2:56
  • \$\begingroup\$ @Chris Aaker Good question. This link answers that stackoverflow.com/questions/5682106/… \$\endgroup\$
    – user7212
    Sep 20, 2011 at 2:59
  • 2
    \$\begingroup\$ Please don't edit the question to make the existing answers meaningless. If you have new versions of the code post a new question. Reference this one (in it's original form) and explain that it's a new issue. meta.stackexchange.com/questions/94446/… meta.stackexchange.com/questions/64459/… \$\endgroup\$
    – palacsint
    Jan 3, 2012 at 21:11

1 Answer 1

2
\$\begingroup\$

First of all, as mentioned by @user7212, use php filter_var() for email validation. Please see below for example:

/**
 * Validate email
 *
 * @param string $email
 *
 * @return bool
 */
public static function email($email)
{
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}

Second, why not naming your function params? If the email validation function would be called $email, if would be much easier to understand then $a.

in_array() function, according to it's documentation, returns bool. Why converting it to int? The result of the check should be bool.

In the pass() function, are you checking a string or a hash? Which brings us to the following item - write comments for your code! PHPDoc is a good example on how to do that.

And the last, but not the least - the name of the class. Check is not the most appropriate name. I would consider Validation or Valid.

Hope this helps.

\$\endgroup\$
1
  • \$\begingroup\$ Please tick the answer if it helped ;) \$\endgroup\$
    – t1gor
    Sep 6, 2017 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.