SE Positioning  |   Databases  |   Site Reviews  |  Portfolio  |   Site Maintenance  |   Home  |   Contact
 

Required Fields in PHP & Email validation

I found the following at benjaminkeen.com and have made it more clear for beginners! You can also validate a number of other fields in addition to email addresses. Note: The process for making sure the email address is a real address is more complex, and is well explained by zend.com

  1. Right click to download the file validation.php and put it on your webserver in the directory where it will be used.

  2. Right click to save and then insert the code php_req_field_code_head.txt in your php form page above the opening html tag.

  3. bk.com says, "The overall logic of the code is simple: if the user has clicked the submit button (with the name="submit" attribute, it submits the contents of the form to the same page. Then, the if (isset($_POST["submit"])) test returns true, and it executes the code in that if-block."
    • Each rules[] = "..." statement adds a new validation rule into the rules list. The statements each contain a single string. The push statements are all of the following form:
      "[if:FIELDNAME=VALUE,]REQUIREMENT,fieldname[,fieldname2[,fieldname3,date_flag]],error message" The square bracket [] notation indicates that the contents are optional.
    • Examination of the Components
      • if:FIELDNAME=VALUE,
        This allows us to only validate a field only if a fieldname FIELDNAME has a value VALUE. This option allows for nesting; i.e. you can have multiple if clauses, separated by a comma. They will be examined in the order in which they appear in the line.
    • REQUIREMENT
      • Valid REQUIREMENT strings are:
        required field must be filled in
        digits_only field must contain digits only
        length=X field has to be X characters long
        length=X-Y field has to be between X and Y (inclusive) characters long
        valid_email field has to be valid email address
        valid_date field has to be a valid date
      • fieldname: MONTH field
        fieldname2: DAY field
        fieldname3: YEAR field
        date_flag: "later_date" / "any_date"
        same_as fieldname is the same as fieldname2 (for password comparison)
        range=X-Y field must be a number between the range of X and Y inclusive
      • Notes: With both digits_only and valid_email options, if the empty string is passed in it won't generate an error, thus allowing validation of non-required fields. So, if you want a field to be a valid email address, provide validation for both "required" and "valid_email".


  4. Right click to save and then insert the code php_req_field_code_body.txt in the body of the php page, before the form begins. You'll notice that you can change the warning message if there are errors in field entry.

  5. Modify your success message by revising the message in the header php line that starts, "$success_message=" in the code in step 4.

  6. Modify the 1st line of the form within the tags, so that it says:
    form action="" method="post" onsubmit="return validateFields(this, fieldInfo)"

  7. Done! You can look at benjaminkeen.com's sample, or see a more simple sample in action in one of my client sites, www.vastu-design.com.




Entire Contents © 2000-2006, PrairieComm, All rights reserved
Phone: 641.472.6011    About PrairieComm.Net