Last active
August 29, 2015 14:07
-
-
Save adammcarth/72f51402b62e80e0d114 to your computer and use it in GitHub Desktop.
Validate form with PHP and Regular Expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Presence check | |
if ($firstname == '' || $lastname == '' || $street == '' || $suburb == '' || $postcode == '' || $email == '' || $status == '' || $dob == '') { | |
$error = 'Your field is empty'; | |
renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error); | |
// Validate Email | |
} elseif (ereg('\S+@\S+\.\S+', $email) == false) { | |
$error = 'Please enter a valid email address.'; | |
renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error); | |
// Validate Postcode | |
} elseif (ereg('^[0-9]{4}$', $postcode) == false) { | |
$error = 'Only 4 digit postcodes are accepted.'; | |
renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error); | |
// Validate Date of Birth | |
} elseif (ereg('^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$', $dob) == false) { | |
$error = 'Your birthday must be in the form of DD/MM/YYYY.'; | |
renderForm($firstname, $lastname, $street, $suburb, $postcode, $email, $status, $dob, $error); | |
} else { | |
... | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lol @ how you have to use
<?php
?>
to start a php script. Forgot about that.