Treehouse: Grow your CSS skills. Land your dream job.

Validating Form with Mandatory Fields

  • # May 16, 2012 at 10:33 am

    I have the contact form from the downloads section of this site on my website. However, I keep getting people clicking send without actually filling out the form. I presume people are just interested to see what happens! Is there any way I can make the fields mandatory so the form cannot be submitted unless they are filled out?

    A template of some sort would be most helpful. My PHP and Javascript skills are very weak!


    # May 16, 2012 at 3:04 pm

    Just wrote this for a friend, its javascript and validates by testing regular expressions. If you need an in depth explanation just let me know and i can comment the shit out of this. But keep in mind that Javascript can be disabled and should have a fallback, server side (php,, etc).

    < !DOCTYPE html>

    # May 16, 2012 at 3:09 pm

    For PHP, this is a test form i played around with a while back == contact.php

    The workin example is on one of my garbage free sits…

    This one, however is not my code. This is a dreamweaver tutorial site or something. Information link:

    – Contact Form Designed by James Brand @ –>

    – Covered under creative commons license – –>

    < ?php

    // Set email variables
    $email_to = '';
    $email_subject = 'Form submission';

    // Set required fields
    $required_fields = array('fullname','email','comment');

    // set error messages
    $error_messages = array(
    'fullname' => 'Please enter a Name to proceed.',
    'email' => 'Please enter a valid Email Address to continue.',
    'comment' => 'Please enter your Message to continue.'

    // Set form status
    $form_complete = FALSE;

    // configure validation array
    $validation = array();

    // check form submittal
    if(!empty($_POST)) {
    // Sanitise POST array
    foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

    // Loop into required fields and make sure they match our needs
    foreach($required_fields as $field) {
    // the field has been submitted?
    if(!array_key_exists($field, $_POST)) array_push($validation, $field);

    // check there is information in the field?
    if($_POST[$field] == '') array_push($validation, $field);

    // validate the email address supplied
    if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);

    // basic validation result
    if(count($validation) == 0) {
    // Prepare our content string
    $email_content = 'New Website Comment: ' . "nn";

    // simple email content
    foreach($_POST as $key => $value) {
    if($key != 'submit') $email_content .= $key . ': ' . $value . "n";

    // if validation passed ok then send the email
    mail($email_to, $email_subject, $email_content);

    // Update form switch
    $form_complete = TRUE;

    function validate_email_address($email = FALSE) {
    return (preg_match('/^[^@s]+@([-a-z0-9]+.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;

    function remove_email_injection($field = FALSE) {
    return (str_ireplace(array("r", "n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));

    < !DOCTYPE html>

    We Appreciate Your Feedback

    < ?php if($form_complete === FALSE): ?>

    Your Name

    < ?php if(in_array('fullname', $validation)): ?>< ?php echo $error_messages; ?>< ?php endif; ?>

    e.g. John Smith

    Your email address

    < ?php if(in_array('email', $validation)): ?>< ?php echo $error_messages; ?>< ?php endif; ?>

    We will not use your email address with anyone

    Your Message

    < ?php if(in_array('comment', $validation)): ?>< ?php echo $error_messages; ?>< ?php endif; ?>

    < ?php else: ?>

    Thank you for your Message!

    < ?php endif; ?>

    # May 16, 2012 at 5:27 pm

    Thanks alot _John_ That was most helpful of you!

    # May 16, 2012 at 7:54 pm

    Your welcome man, kinda perfect timing actually :) Like i said, if you need anything to be clarified just let me know :)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.