Required field will check whether the field is filled or not in the proper way. Most of cases we will use the * symbol for required field.
What is Validation ?
Validation means check the input submitted by the user. There are two types of validation are available in PHP. They are as follows −
Client-Side Validation − Validation is performed on the client machine web browsers.
Server Side Validation − After submitted by data, The data has sent to a server and perform validation checks in server machine.
Some of Validation rules for field
Name | Should required letters and white-spaces |
Should required @ and . | |
Website | Should required a valid URL |
Radio | Must be selectable at least once |
Check Box | Must be checkable at least once |
Drop Down menu | Must be selectable at least once |
Valid URL
Below code shows validation of URL
$website = input[$_POST["site"]]; if [!preg_match["/\b[?:[?:https?|ftp]:\/\/|www\.][-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website]] { $websiteErr = "Invalid URL"; }
Above syntax will verify whether a given URL is valid or not. It should allow some keywords as https, ftp, www, a-z, 0-9,..etc..
Valid Email
Below code shows validation of Email address
$email = input[$_POST["email"]]; if [!filter_var[$email, FILTER_VALIDATE_EMAIL]] { $emailErr = "Invalid format and please re-enter valid email"; }
Above syntax will verify whether given Email address is well-formed or not.if it is not, it will show an error message.
Example
Example below shows the form with required field validation
.error {color: #FF0000;}Absolute classes registration
* required field.