HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

class design improvement - user validation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
userdesignvalidationimprovementclass

Problem

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);
    }
  }

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#4922, answer score: 2

Revisions (0)

No revisions yet.