PHP form validation with highlighted error fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adrock952
    New Member
    • Aug 2007
    • 8

    PHP form validation with highlighted error fields

    I have a form which validates fine using classes but i would like the fields that have the error to be highlighted a different color

    At the moment I just get a list of errors but would like a field that failed validation changed color.

    here is my code

    validator class
    [PHP]<?php
    class Validator {
    var $errors;

    function Validator($vali dateThis)
    {
    $this->errors = array();
    $this->validate($vali dateThis);
    }

    function validate($valid ateThis) {}

    function setError($msg)
    {
    $this->errors[] = $msg;
    }

    function isValid()
    {
    if (count($this->errors) > 0) {
    return FALSE;
    } else {
    return TRUE;
    }
    }

    function fetch()
    {
    $error = each($this->errors);
    if ($error) {
    return $error['value'];
    } else {
    reset($this->errors);
    return FALSE;
    }
    }
    }
    ?>[/PHP]

    [PHP]this is one of my validation classes
    <?php
    require_once 'validators/Validator.php';

    class ValidatePostcod e extends Validator {
    function validate($postc ode)
    {
    if(empty($postc ode)) {
    $this->setError('Post code field empty');
    }
    else {
    if(!preg_match( "/^([Gg][Ii][Rr]0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))){0,1}[0-9][A-Za-z]{2})$/",$postcode )) {
    $this->setError('Post code in invalid format');
    }
    }
    }
    }
    ?>[/PHP]

    this is how i create a new instance of the class with the input from the form
    [PHP]$validators[]=new ValidatePostcod e($_POST['postcode']);[/PHP]

    This is how i used to change the color of the input field
    [PHP]<input type="text" <?php error_bool($err or, "postcode") ; ?>[/PHP]
    with this function
    [PHP]function error_bool($err or, $field) {
    if($error[$field]) {
    print("style=\" background-color:#E7EFFB\" ");
    }
    else {
    print("style=\" background-color:white\"") ;
    }
    }[/PHP]
  • aktar
    New Member
    • Jul 2006
    • 105

    #2
    Could you post the errors so we can have a look?

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Do you want to highlight the text entry field. like you do changing the background color, or do you want to highlight the field description of the field-in-error?

      In the latter case you'll have to pass the field description in the validator call and set a new array in your class that can hold that name until printed out along with the error message.

      Ronald

      Comment

      • Adrock952
        New Member
        • Aug 2007
        • 8

        #4
        I would like the text box to change color so they can easily see which text boxes have the errors.

        Basically, the form fields are white until the field fails validation then they are changed color. Once the field is validated it changes back to white.

        If you need to see any more code I can post it.....also I have no errors....i just need to know how to change the color

        Many thanks for your replies

        Comment

        Working...