Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Form Validation and Initial Field Values…

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #34892
    charlie
    Participant

    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:

    <form action="contact.php" method="post" name="packagingdesignthatsells"><br />
    <br />
    <p class="form-labels"><span id="sprytextfield2"><span class="textfieldRequiredMsg">Your name is required.</span><input name="name" type="text" class="fields" onfocus="if(this.value == 'Name*') { this.value = ''; }" value="Name*"></span></p><br />
    <p class="form-labels"><span id="sprytextfield3"><span class="textfieldRequiredMsg">A valid email is required.</span><input name="email" type="text" class="fields" onfocus="if(this.value == 'Email*') { this.value = ''; }" value="Email&#42"></span></p><br />
    <p class="form-labels"><span id="sprytextfield4"><span class="textfieldRequiredMsg">Your phone number is required.</span><input name="phone" type="text" class="fields" onfocus="if(this.value == 'Phone*') { this.value = ''; }" value="Phone&#42"> </span></p><br />
    <p class="form-labels">Optional<textarea name="message" cols="20" rows="8" class="text-fields" onfocus="this.value=''; this.onfocus=null">Have a particular project in mind?<br />
    Give us a few details on how we might help...</textarea></p><br />
    <br />
    <input name="SEND" type="submit" class="send" value="GET STARTED NOW" ><br />
    <input type="hidden" name="Recipient" value="email"><br />
    <input type="hidden" name="Subject"   value="CONTACT www.URL.com"><br />
    <input type="hidden" name="Redirect"  value="/thanks.html"><br />
    <br />
    </form><br />
    

    See PHP (very messy):

    <?php<br />
    <br />
    $thisRecip = $_POST ;<br />
    <br />
    // $thisRecip = '[email protected]' ;<br />
    <br />
    $thisEmail = $thisRecip ;<br />
    if( !empty($_POST) ) $thisEmail = $_POST ;<br />
    <br />
    $to      = $thisRecip ;<br />
    $subject = $_POST ;<br />
    $headers = "From: $thisEmail" . "rn" .<br />
    'X-Mailer: PHP/' . phpversion() ;<br />
    <br />
    $body  = "Name            = " . $_POST . "n" ;<br />
    $body .= "Email            = " . $_POST . "n" ;<br />
    $body .= "Phone           = " . $_POST . "n" ;<br />
    $body .= "Message        = " . wordwrap($_POST,70) . "n" ;<br />
    <br />
    <br />
    mail($to, $subject, $body, $headers) ;<br />
    header('Location:' . $_POST);<br />
    <br />
    ?><br />
    

    Thank you,
    CM

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

    #89542
    Eric
    Member
    #89547
    charlie
    Participant

    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

    #89549
    standuncan
    Member

    I am sort of dealing with the same issue here:
    https://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:
    https://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).

    #89551
    charlie
    Participant

    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!

    #89554
    charlie
    Participant

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

    #89561
    standuncan
    Member

    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<br />
    if(empty($email)){<br />
    $errors = '<span class="error">Enter your email</span>';<br />
    } else{<br />
    <br />
    // First, we check that there's one @ symbol,<br />
    // and that the lengths are right.<br />
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {<br />
    // Email invalid because wrong number of characters<br />
    // in one section or wrong number of @ symbols.<br />
    $errors = '<span class="error">Invalid email</span>';<br />
    }<br />
    <br />
    // Split it into sections to make life easier<br />
    $email_array = explode("@", $email);<br />
    $local_array = explode(".", $email_array[0]);<br />
    for ($i = 0; $i < sizeof($local_array); $i++) {<br />
    <br />
    if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&<br />
    '*+/=?^_`{|}~.-]{0,63})|("[^(\|")]{0,62}"))$",<br />
    $local_array[$i])) {<br />
    <br />
    $errors = '<span class="error">Invalid email</span>';<br />
    }<br />
    }<br />
    <br />
    // Check if domain is IP. If not,<br />
    // it should be valid domain name<br />
    if (!ereg("^[?[0-9.]+]?$", $email_array[1])) {<br />
    <br />
    $domain_array = explode(".", $email_array[1]);<br />
    <br />
    if (sizeof($domain_array) < 2) {<br />
    $errors = '<span class="error">Invalid email</span>';<br />
    }<br />
    <br />
    for ($i = 0; $i < sizeof($domain_array); $i++) {<br />
    if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|<br />
    ([A-Za-z0-9]+))$",<br />
    $domain_array[$i])) {<br />
    $errors = '<span class="error">Invalid email</span>';<br />
    }<br />
    }<br />
    }<br />
    }<br />
    
Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Other’ is closed to new topics and replies.