Grow your CSS skills. Land your dream job.

Form Validation and Initial Field Values…

  • # October 24, 2011 at 1:07 pm

    I’ve set-up a form that uses js validation and sends email with php. In the form fields I have initial values specified such as Name, email, etc. The problem is you can just click the send button and the initial values meet the validation requirements so the email is sent.

    Can the initial filed values be made to NOT validate using js or php?

    See html form code:




    Your name is required.


    A valid email is required.


    Your phone number is required.


    Optional









    See PHP (very messy):


    < ?php

    $thisRecip = $_POST ;

    // $thisRecip = 'name@url.com' ;

    $thisEmail = $thisRecip ;
    if( !empty($_POST) ) $thisEmail = $_POST ;

    $to = $thisRecip ;
    $subject = $_POST ;
    $headers = "From: $thisEmail" . "rn" .
    'X-Mailer: PHP/' . phpversion() ;

    $body = "Name = " . $_POST . "n" ;
    $body .= "Email = " . $_POST . "n" ;
    $body .= "Phone = " . $_POST . "n" ;
    $body .= "Message = " . wordwrap($_POST,70) . "n" ;


    mail($to, $subject, $body, $headers) ;
    header('Location:' . $_POST);

    ?>

    Thank you,
    CM

    P.S.
    Wasn’t sure what category to put this question in.

    # October 24, 2011 at 2:34 pm

    Your welcome http://www.visibilityinherit.com/code/jquery-infieldlabel.php

    # October 24, 2011 at 2:55 pm

    Thanks for the info! I was hoping to avoid adding js to these pages. It seem like overkill.

    Could there be a php solution or a way to set the validation to include a requirement that input must contain certain characters or a certain number of characters to validate? (ie. email must contain “@” or phone must contain only numbers)

    CM

    # October 24, 2011 at 3:10 pm

    I am sort of dealing with the same issue here:
    http://css-tricks.com/forums/discussion/14225/jquery-placeholder-in-ie-passing-validation…#Item_4

    You can also see the php validation I put together for the email input here:
    http://css-tricks.com/forums/discussion/14161/php-form-validation-example.-secure-or-not-secures#Item_5

    As far as only numbers, you can search is_numeric on php.net and that should help out.
    http://www.php.net/manual/en/function.is-numeric.php
    or for how many characters you want look up strlen (string length).

    # October 24, 2011 at 3:54 pm

    What is the validation checking? (just the word “email”):


    } else {
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    $errors = 'Invalid email';
    }

    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++) {

    if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
    '*+/=?^_`{|}~.-]{0,63})|("[^(\|")]{0,62}"))$",
    $local_array[$i])) {
    $errors = 'Invalid email';
    }
    }

    Thank you,
    This is great!

    # October 24, 2011 at 4:12 pm

    Is there such an attribute as minlength=””? to use in input field?

    # October 24, 2011 at 4:47 pm

    The I think the there is only a HTML5 maxlength attribute, but again if you use it in the php validation you can check against the strlen.

    Here is a snippet of the email validation with comments for you:



    //Check the form fields
    if(empty($email)){
    $errors = 'Enter your email';
    } else{

    // First, we check that there's one @ symbol,
    // and that the lengths are right.
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters
    // in one section or wrong number of @ symbols.
    $errors = 'Invalid email';
    }

    // Split it into sections to make life easier
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++) {

    if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
    '*+/=?^_`{|}~.-]{0,63})|("[^(\|")]{0,62}"))$",
    $local_array[$i])) {

    $errors = 'Invalid email';
    }
    }

    // Check if domain is IP. If not,
    // it should be valid domain name
    if (!ereg("^[?[0-9.]+]?$", $email_array[1])) {

    $domain_array = explode(".", $email_array[1]);

    if (sizeof($domain_array) < 2) {
    $errors = 'Invalid email';
    }

    for ($i = 0; $i < sizeof($domain_array); $i++) {
    if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
    ([A-Za-z0-9]+))$",
    $domain_array[$i])) {
    $errors = 'Invalid email';
    }
    }
    }
    }

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

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".