473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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($validateThis)
{
$this->errors = array();
$this->validate($validateThis);
}

function validate($validateThis) {}

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 ValidatePostcode extends Validator {
function validate($postcode)
{
if(empty($postcode)) {
$this->setError('Postcode 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('Postcode in invalid format');
}
}
}
}
?>[/PHP]

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

This is how i used to change the color of the input field
[PHP]<input type="text" <?php error_bool($error, "postcode"); ?>[/PHP]
with this function
[PHP]function error_bool($error, $field) {
if($error[$field]) {
print("style=\"background-color:#E7EFFB\"");
}
else {
print("style=\"background-color:white\"");
}
}[/PHP]
Mar 29 '08 #1
3 2520
aktar
105 100+
Could you post the errors so we can have a look?
Mar 29 '08 #2
ronverdonk
4,258 Expert 4TB
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
Mar 29 '08 #3
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
Mar 30 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
5
by: Red | last post by:
Hi, I'm not very familiar with Javascript. I usually leave that kind of stuff up to Dreamweaver, but i'm starting to need a little more than it can offer. I have an asp page which creates a...
2
by: TCORDON | last post by:
Is there a way to make the form validation highlight the fields where the errors occur? (change the backcolor of the textboxes)? And second, perform validation without postback and run some...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
8
by: Phil Latio | last post by:
I've been creating an application over the last few weeks and generally pleased with what I have produced but one area is irritating me, form validation. At the moment the forms are simply...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
3
by: Adrock952 | last post by:
I have been trying for ages to work out how to highlight a form field using css when there is an error during validation. I have got the form to validate fine but i don't know how to change the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.