Results 1 to 8 of 8

Thread: PHP Form Validation

  1. #1
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Form Validation

    Hi,
    I'm new to PHP and I'm struggling with a project that requires form validation. The requirements are all fields in the form are required, error message should appear if fields are not completed or completed incorrectly. Phone and email should be in proper format. A confirmation page should appear, thanking the visitor and the page will include all information just entered.

    Entered data is written to a file and an email is sent to myself and the visitor. The visitor's email should confirm the info and request a reply.

    I've searched tons of resources and I have no idea what I'm doing. I've created the form already.


    Here's the form:
    Code:
      <form id="contact" name="contact" method="post"  class="form">
           
           <p> 
              <label for="fname">First Name</label>
              &nbsp;
              <input name="fname" type="text" id="fname" size="50" />
           </p>
            
              <label for="lname">Last Name</label>
              &nbsp;          
              <input name="lname" type="text" id="lname" size="50" />
            
           <p>
               <label for="title">Job Title</label> 
               &nbsp;
               &nbsp;
         <input name="title" type="text" id="title" size="50" />
            </p>
            <p>
            	<label for="company">Company</label>
                <input name="company" type="text" id="company" size="50" />
            </p>
               <p>
              <label for="address">Mailing Address</label>
             
              <input type="text" name="address" id="address" size="50" /> <br />
              &nbsp;&nbsp; &nbsp;
              &nbsp;&nbsp;
              &nbsp;&nbsp;
              &nbsp;&nbsp;
              &nbsp;&nbsp;
              &nbsp;&nbsp;
              &nbsp;&nbsp;
              
              
              
              <input type="text" name="address" id="address" size="50" />
            </p>
            <p>
              <label for="city">City</label>
              <input type="text" name="city" id="city" size="25" />
              <label for="zip">Zip Code</label>
              <input type="text" name="zip" id="zip" size="10" />
              <label for="state">State</label>
              <input type="text" name="state" id="state" size="2" />
            </p>
            
            <p>
            	<label for="phone">Phone Number</label>
                <input type="text" name="phone" id="phone" size="3" />
                <input type="text" name="phone" id="phone" size="3" />
                <input type="text" name="phone" id="phone" size="4" />
                <label for="fax">Fax Number</label>
                <input type="text" name="fax" id="fax" size="3" />
                <input type="text" name="fax" id="fax" size="3" />
                <input type="text" name="fax" id="fax" size="4" />
            </p>
             
             <p>
             	<label for="email">Email Address</label>
                <input type="text" name="email" id="email" size="50" />
            <p> Enter any additional comments: <br />
                <textarea name="comment" cols="40" rows="5"></textarea>
            </p>
            <p>
              <input type="submit" name="submit" value="Submit"/>
              <input name="reset" type="reset" value="Reset" />
            </p>
          </form>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    plug the value into preg_match:
    Code:
    if(preg_match('/putPatternHere/', $valueToTest)){ echo 'good value.'; }else{ echo 'bad value!'; }
    to check if something is not empty: .+ (means 1 or more of any character)
    lotsa ways to check email addresses.
    phone numbers... well, what do you mean by "proper format"?
    ...1-234-567-8901
    ...1(234)567-8901
    ...1.234.567.8901
    ...(234)567-8901
    ...567-8901
    many more possibilities, none of which are "wrong", exactly. And that's not counting country codes, etc.

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    <?php
    $test='abc@domain.info';
    if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/', 
            $test)) {echo "YES";}
    else {echo "NO";}
    ?>
    should check most emails. traq is right in that there is no "proper format", but some are better than others and there is none yet that can check all email types, but the above should match most and is simple.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry I didn't explain further. By proper format I am required to ensure that the numbers are 234-567-8901 and the email should be xxx@xxxxxxx.xxx

    If I use what James has provided that will check email correct? And then for each other field in my form I should use the code that traq provided, correct? Now my question is how do I get it to show a confirmation page, write the data to a text file, send an email to the visitor to confirm the info and then send an email to myself?

    Also, should this code be placed in an external file? Sorry for all the questions.

    By the way, thanks for you help!

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Just answering some of your questions, my regex should check most emails. What traq posted is the basic framework for checking phone numbers. Here is an example of one that will check phone numbers. In particular it checks US phone numbers and puts it into the format you mentioned, if it is a US phone number that is. I got it from php.net and you can read more about it here.


    Detect and reformat phone numbers:
    PHP Code:
    <?php
    $phoneNumber
    ="337-333-3443";
    $regex '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})' 
            
    .'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})' 
            
    .'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';

    if (
    preg_match($regex$phoneNumber))

    {
    $formatted preg_replace($regex'$1-$2-$3'$phoneNumber);
    echo 
    "$formatted";}
    else echo 
    "NOT A MATCH";
    ?>
    You can write the info into a text file, but depending on your needs it is usually better to store the data into a database. Sending confirmation emails is not something I know how to do.

    What do you mean by a confirmation page? Do you mean that you want to submit the user submitted data to another page and if the submitted data is not valid then go back to the previous page? I am also curious what you mean by an external file. Do you mean like an 'include' file? That might not be a bad idea, but not necessary. It is mostly a way to make managing code easier and quicker.

    Well, at least we now have a regex to detect emails and another for US phone numbers
    Last edited by james438; 11-21-2009 at 03:32 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    For the external file I was talking about an include page. I thought I had to put it in another file then call the file or something like that. If you don't use the include page then do you just paste the code directly into the html document? Is there a certain area that the code goes?

    The assignment instructions are asking for a confirmation page. I either need the confirmation page to be either in email format that is sent to the visitor or a page that appears after the form is submitted.

  7. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    You can post php code anywhere into an html document provided you begin your php statements with <? or <?php and end your php statements with ?>. You should also save your documents with a .php extension. The extensions determine in certain cases how the file will be used. I seem to remember a file with an html extension executing php scripts, but I suspect that in order to get that to work you need to mess with the php.ini file at the very least or have the use of a dedicated server, which is more advanced and expensive than I care to get.

    Here is an example:
    PHP Code:
    <?php $genre="data"?>
    <input type="text" name="genre" cols=45
            value="<?php print $genre;?>">
    In this case I suggest creating your html form and submitting the data to another page using $_POST where the data will be examined and processed if need be. Then if the data is bad you can run a short javascript to redirect the person back to the html form page to try again, finish, and/or edit their data.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. #8
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    The following pages might be helpful:
    PHP form validation script
    and
    PHP form tutorial: first steps

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •