Created
January 16, 2013 09:45
-
-
Save johnsardine/4545951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// <form name="register" ...> | |
if($_POST['register']) | |
{ | |
$email_regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; | |
$validity = array( | |
'password_ok' => preg_match("/^[a-z0-9_-]*$/i", $pass) | |
&& (strlen($pass < 6) || strlen($pass > 30)), | |
'password_match' => ($_POST['pass'] === $_POST['rpass']), | |
'username_valid' => preg_match("/^[a-z0-9._-]*$/i", ($_POST['user'])), | |
'email_valid' => preg_match($email_regex, $_POST['email']) | |
); | |
$errors = array(); | |
foreach ($validity as $key => $is_valid) | |
{ | |
if(!$is_valid) | |
switch ($key) { | |
case 'password_ok': | |
$errors[] = 'Invalid Password'; | |
break; | |
case 'password_match': | |
$errors[] = 'Password do not match'; | |
break; | |
case 'username_valid': | |
$errors[] = 'Invalid Username'; | |
break; | |
case 'email_valid': | |
$errors[] = 'Invalid Email'; | |
break; | |
} | |
} | |
if( count($errors)==0 ) | |
//insert to database | |
} | |
// HTML | |
<? | |
if( count($errors)>1 ) | |
foreach ($errors => $error) | |
echo $error.'<br/>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment