Jump to content

Form Validation Tips For All! Part One.


jadedknight

Recommended Posts

I hope this is the right forum to post this in.

Ok guys and gals. I have read these forums a lot looking at posts and gaining a lot of information about PHP. I just want to give something back. When I begin writing code I always wonder if it's the most efficient way to write it, and if it is using proper semantics. So I purchased the PHP Cookbook http://www.oreilly.com/catalog/phpckbk/ and I wanted to share some of the code with you. All these code snippets have to do with form validation and processing because that is one of the most popular subjects of PHP. I don't want to rip off the book so I will post the code but try and write my own descriptions. Please enjoy!

 

First thing I would do is take a look here http://www.php.net/manual/en/language.basic-syntax.php at advanced escaping, I never knew you could write PHP split up like that!

Problem: You want to use the same page to process and emit a form.

 

<?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { ?>
<form action = "<?php echo $_SERVER['SCRIPT_NAME'] ?>" method = "post">
What is your first name?
<input type = "text" name = "first_name" />
<input type = "submit" value = "Say Hello" />
</form>
<?php } else {
echo 'Hello, ' . $_POST['first_name'] . '!';
}
?>

 

Problem: Make sure a value has been supplied in a form.

 

<?php
if (! strlen($_POST['flavor'])) {
print 'You need to supply a value!';
}
?>

** OR **

Strict validation for empty fields.

<?php
if (! (isset($_POST['flavor']) && strlen($_POST['flavor']))) {
print 'You must enter a value!';
}
?>

 

Problem:Looking for a digit in a form NOT a letter.

 

<?php
if (! ctype_digit($_POST['age'])) {
print 'Please enter a digit!';
}
?>

 

Heres a gem.

Problem: Valid e-mail required!

 

<?php function is_valid_email_address($email){
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c' . '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext | $quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext | $quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref | $domain_literal)";
$word = "($atom | $quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$addr_spec = "$local_part\\x40$domain";
return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
}

if (is_valid_email_address('hello@hello.com')) {
print 'Valid';
} else {
print 'Not Valid';
}
?>

 

Anyways this is form validation part one. More parts coming soon!! I hope I help. Btw! I REALLY reccomend this book, I just purchased it tonight it's great. Very practical code examples.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.