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;
}
}
}
?>
Results 1 to 5 of 5
Thread: [PHP] Checking class.
- 19 Feb. 2011 08:59pm #1
[PHP] Checking class.
Last edited by HTML; 23 Feb. 2011 at 06:29pm.
- 23 Feb. 2011 06:30pm #2
Updated and made it more user friendly, no more having to edit the class to use it the way you want.
- 24 Feb. 2011 02:33am #3
I'm not sure if PHP has this (it should though), when you use Regex there are shortcuts to characters, for example, in Python you can use \s to match any whitespace character (tab, space, and even newline characters!).
It will shorten your patterns, drastically.
- 25 Feb. 2011 05:08am #4
- 10 Apr. 2011 12:20pm #5
Thanks a million was looking for this on my acc creator