Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Last active August 29, 2015 14:07
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 adammcarth/72f51402b62e80e0d114 to your computer and use it in GitHub Desktop.
Save adammcarth/72f51402b62e80e0d114 to your computer and use it in GitHub Desktop.
Validate form with PHP and Regular Expressions
<?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 {
...
}
?>
@adammcarth
Copy link
Author

Lol @ how you have to use <?php ?> to start a php script. Forgot about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment