array(
"/^([a-z]|[A-Z]){6,20}$/",
"Username should have letters only and should be atleast 6 letters",
),
"firstname" => array(
"/^([a-z]|[A-Z]){1,20}$/",
"Firstname should have letters only",
),
"lastname" => array(
"/^([a-z]|[A-Z]){1,20}$/",
"Lastname should have letters only",
),
"email" => array("/^.*@.*\..*$/", "Email should be in right format!"),
"phone" => array("/^\d{10}$/", "Phone number should be numeric and 10 digits"),
"password" => array(
"/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*\W)(?!.* ).{8,16}$/",
"Password must be 8 letters or more, contains lowercase, uppercase, digit and special characters",
),
);
function validate() {
$validators = $GLOBALS['validators'];
$required_fields = $GLOBALS['required_fields'];
$err = "";
foreach($required_fields as $f) {
if(!array_key_exists($f, $_POST) || strlen($_POST[$f]) <= 0) {
$err = $err . "
" . $f . " is required!";
}
}
$err = $err . "
";
foreach($_POST as $key => $val) {
if(array_key_exists($key, $validators)
&& !preg_match($validators[$key][0], $val)) {
$err = $err . "
" . $validators[$key][1];
}
}
if(strlen($err) > 0) {
echo $err;
return;
}
}
if($_SERVER["REQUEST_METHOD"] == "POST") {
validate();
echo "Registered!";
return;
}
?>