Skip to content

Instantly share code, notes, and snippets.

@majidmushtaq91
Created March 26, 2015 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majidmushtaq91/07163f32cb13547d9e87 to your computer and use it in GitHub Desktop.
Save majidmushtaq91/07163f32cb13547d9e87 to your computer and use it in GitHub Desktop.
PHP - Validate password limit and special characters.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<?php if($_POST) {
function validate_input($val = "") {
$output = "";
$validate = preg_match('/^[-a-zA-Z0-9 .]+$/', $_POST["password"]);
$validate2 = preg_match('/[@.?(),!-]/', $_POST["password"]);
if(strlen($_POST["password"]) < 8 || strlen($_POST["password"]) > 12) {
$output = "Please enter password between 8-12";
}
if(!$validate && !$validate2) {
$output = "This Special character not allowed";
}
return $output;
}
echo validate_input($_POST);
}
?>
<form action="" method="POST">
<input type="password" name="password" id="password" >
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment