Checks for valid email address.
Allows users to set username length.
Check password strength, password and allows users to specify how long the password must be, though they still need one cap letter and one number.
I used Regex over using random PHP functions.
PHP Code:
/*
* checking class written and updated by HTML AKA: TONY.
*
*
*/
class Check
{
public $error;
public $length;
public $plength;
/** Can be edited if needed, add more I'll add more soon (: */
private $username;
private $password;
private $password2;
private $email;
public function __construct($name, $pass, $email, $pass2){
$this->username = $name;
$this->password = $pass;
$this->email = $email;
$this->password2 = $pass2;
}
/** This function will run ALL functions at their defaults, after checking if your feilds are set. Use the functions seperate if you're not looking
for this excatly*/
public function RunAll(){
if(empty($this->username) || empty($this->password) || empty($this->email)){
return "<center>All feilds are required to continue!</center>";
}
else{
echo $this->Matchpass();
echo $this->UserLength('4,20');
echo $this->EmailVer();
echo $this->PassStr(8);
}
}
/**If you have to users to verify their passwords, use this function.*/
public function Matchpass(){
if($this->password !== $this->password2){
return "<center>Your passwords do not match!</center>";
}
}
/** This function checks to make sure the username is however lonh you wish. */
public function UserLength($length)
{
$this->length = $length;
$pattern = '/^[a-zA-Z0-9._-]{' . $this->length . '}$/';
if(preg_match($pattern, $this->username)){
/** Then their username is perfect! */
}
else{
$this->error = "<center>Your username must be at least $this->length characters long.</center> <br />";
return $this->error;
}
}
/** This function checks that they have submitted a valid email address.*/
public function EmailVer()
{
$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
if(preg_match($pattern, $this->email)){
/** Email is perfect move on! */
}
else{
$this->error = "<center>Please enter a valid emaill address.</center> <br />";
return $this->error;
}
}
/** The password must be at least $var chars with one capital letter and one number.*/
public function PassStr($plength)
{
$this->plength = $plength;
$pattern = "/^.*(?=.{' . $this->plength . '})(?=.*[a-zA-Z0-9._-]+[A-Z0-9])/";
if(preg_match($pattern, $this->password)){
/** Password is perfect move on! */
}
else{
$this->error = "<center>Your password must be $this->plength characters long and have one capital letter and at least one number! </center><br />";
return $this->error;
}
}
}
?>