126 lines
3.7 KiB
PHP
126 lines
3.7 KiB
PHP
<?php
|
|
|
|
$required_fields = array("username", "firstname", "lastname", "gender", "email", "phone", "password");
|
|
|
|
$validators = array(
|
|
"username" => 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 . "<br>" . $f . " is required!";
|
|
}
|
|
}
|
|
|
|
$err = $err . "<br><br>";
|
|
|
|
foreach($_POST as $key => $val) {
|
|
if(array_key_exists($key, $validators)
|
|
&& !preg_match($validators[$key][0], $val)) {
|
|
$err = $err . "<br>" . $validators[$key][1];
|
|
}
|
|
}
|
|
|
|
if(strlen($err) > 0) {
|
|
echo $err;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
validate();
|
|
echo "Registered!";
|
|
return;
|
|
}
|
|
?>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
<link rel="stylesheet" href="style.css" />
|
|
</head>
|
|
|
|
<body>
|
|
<h2>Register:</h2>
|
|
<form action="register.php" method="post">
|
|
<label for="username">Username:</label>
|
|
<input type="text" name="username" placeholder="jadoo" />
|
|
<label for="firstname">First Name:</label>
|
|
<input type="text" name="firstname" placeholder="Jadoo" />
|
|
<label for="lastname">Last Name:</label>
|
|
<input type="text" name="lastname" placeholder="Wala" />
|
|
|
|
<label>Gender:</label>
|
|
<input type="radio" id="male" name="gender" />
|
|
<label for="male" style="width: fit-content; display: inline">Male</label>
|
|
<input type="radio" id="female" name="gender" />
|
|
<label for="female" style="width: fit-content; display: inline">Female</label>
|
|
|
|
<label for="bio">Bio:</label>
|
|
<textarea rows="3" cols="30" name="bio"></textarea>
|
|
|
|
<label for="pref">Preference:</label>
|
|
<select id="pref">
|
|
<option value="vegetable">Vegetable</option>
|
|
<option value="fruits">Fruits</option>
|
|
</select>
|
|
|
|
<label for="email">Email:</label>
|
|
<input type="text" name="email" placeholder="jadoo@email.com" />
|
|
<label for="phone">Phone:</label>
|
|
<input type="text" name="phone" placeholder="9876543210" />
|
|
<label for="password">Password:</label>
|
|
<input type="password" name="password" placeholder="********" />
|
|
<label style="font-style: italic; color: red" id="msg"></label>
|
|
|
|
<label for="getmails" style="width: fit-content; display: inline">Interested to get emails</label>
|
|
<input type="checkbox" name="getmails" id="getmails" />
|
|
|
|
<br />
|
|
<input type="submit" value="Register" />
|
|
</form>
|
|
|
|
<script>
|
|
window.onload = () => {
|
|
alert("Fill everything carefully");
|
|
};
|
|
|
|
document.querySelectorAll("label").forEach((e) => {
|
|
if (e.innerHTML == "Male" || e.innerHTML == "Female") return;
|
|
e.onclick = () =>
|
|
alert(
|
|
"You have to give your " +
|
|
e.innerHTML.replace(":", "").toLowerCase() +
|
|
" here.",
|
|
);
|
|
e.onmouseover = () => (e.style.color = "#4ade00");
|
|
e.onmouseout = () => (e.style.color = "black");
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|