Day 8: Form Validations in PHP
Form validations are used to filter valid data from form. Below are the examples of form validation with regular expression. These are also called server side validations.
Program 29: Required field validation.
$msg = “”;
if(isset($_REQUEST[‘btnSubmit’])) {
$name = trim($_REQUEST[‘txtName’]);
$email = trim($_REQUEST[‘txtEmail’]);
$mobile = trim($_REQUEST[‘txtMobile’]);
$message = trim($_REQUEST[‘txtMessage’]);
if($name==””) {
$msg = $msg . “Name is required!<br/>”;
}
if($email==””) {
$msg = $msg . “Email is required!<br/>”;
}
if($mobile==””) {
$msg = $msg . “Mobile is required!<br/>”;
}
if($message==””) {
$msg = $msg . “Message is required!<br/>”;
}
if($msg == “”) {
/* write your sucess code. */
}
}
?>
<html>
<head>
<style>
.validate { color:red; padding:8px; }
</style>
</head>
<body>
<div class=”validate”>
<?php echo $msg; ?>
</div>
<form method=”POST”>
Name: <input type=”text” name=”txtName”> <br>
Email : <input type=”text” name=”txtEmail”> <br>
Mobile : <input type=”text” maxlength=”10″ name=”txtMobile”> <br>
Message : <textarea name=”txtMessage”></textarea> <br> <br>
<input type=”submit” value=”SUBMIT” name=”btnSubmit”>
</form>
</body>
</html>
Note: This is the simple require field expression, without regular expression.
Program 30: Regular expression in PHP.
$msg = “”;
if(isset($_REQUEST[‘btnSubmit’])) {
$val = trim($_REQUEST[‘txtVal’]);
if(preg_match(“/abc/”, $val)){
$msg = “Value is in format!<br/>”;
} else {
$msg = “Value is not in format!”;
}
}
?>
<html>
<head>
<style>
.validate { color:red; padding:8px; }
</style>
</head>
<body>
<div class=”validate”>
<?php echo $msg; ?>
</div>
<form method=”POST”>
Enter Value: <input type=”text” name=”txtVal”> <br><br>
<input type=”submit” value=”SUBMIT” name=”btnSubmit”>
</form>
</body>
</html>
Note: In above example preg_match(“/abc/”, $val) function is used to check the pattern, in which first argument is pattern and second argument is value. Perg_match will return true only if value will be ‘abc’ else it will return false, means in place of abc we can write our different patterns. Below are the different patters with explanations and examples. Write the expression in place of ‘abc’ and find results.
Expression | Description |
[] | Group |
[0-9] | Matches any decimal digit from 0 through 9. |
[a-z] | Matches any character from lowercase a through lowercase z. |
[A-Z] | Matches any character from uppercase A through uppercase Z. |
[a-zA-Z0-9] | Matches alpha numeric values |
Program 31: Match alpha numeric values.
$msg = “”;
if(isset($_REQUEST[‘btnSubmit’])) {
$val = trim($_REQUEST[‘txtVal’]);
if(preg_match(“/[a-zA-Z0-9]/”, $val)){
$msg = “Pattern matched!”;
} else {
$msg = “Please enter alpha numeric value only!”;
}
}
?>
<html>
<head>
<style>
.validate { color:red; padding:8px; }
</style>
</head>
<body>
<div class=”validate”>
<?php echo $msg; ?>
</div>
<form method=”POST”>
Enter Value: <input type=”text” name=”txtVal”> <br><br>
<input type=”submit” value=”SUBMIT” name=”btnSubmit”>
</form>
</body>
</html>
Note: In above example only alpha numeric values are acceptable in textbox.
Some other basic expressions.
Expression | Description |
a+ | Matches string that containing at least one or more a. |
a* | Matches string that containing at zero or any number of a. |
a? | Matches string that containing zero or only one a. |
a{N} | Matches any string containing a sequence of N a’s |
a{2,3} | Matches any string containing a sequence of two or three a’s. |
a{2, } | Matches any string containing a sequence of at least two a’s. |
a$ | Matches any string with a at the end of it. |
^a | Matches any string with a at the beginning of it. |
\d | Allow decimals only. Equals to [0-9]. |
\D | Not allow decimals. |
\w | Allow alpha numeric values. Equal to [a-zA-Z0-9]. |
\W | Not allow alpha numeric values. Allow special characters only. |
\ | Like |
Program 32: Regular expression for email id.
$msg = “”;
if(isset($_REQUEST[‘btnSubmit’])) {
$val = trim($_REQUEST[‘txtVal’]);
if(preg_match(“/[\w]+\@[\w]+\.[\w]+/”, $val)){
$msg = “Pattern matched!”;
} else {
$msg = “Please enter valid email!”;
}
}
?>
<html>
<head>
<style>
.validate { color:red; padding:8px; }
</style>
</head>
<body>
<div class=”validate”>
<?php echo $msg; ?>
</div>
<form method=”POST”>
Enter Value: <input type=”text” name=”txtVal”> <br><br>
<input type=”submit” value=”SUBMIT” name=”btnSubmit”>
</form>
</body>
</html>