473,809 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP form validation with highlighted error fields

8 New Member
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]
Mar 29 '08 #1
3 2548
aktar
105 New Member
Could you post the errors so we can have a look?
Mar 29 '08 #2
ronverdonk
4,258 Recognized Expert Specialist
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
Adrock952
8 New Member
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
8761
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 if they left any field blank. My guess is that someone is trying to hack the site using the form to gain entry or run commands -- I don't really know since I'm not a hacker. I just know that forms are often susceptible to these kinds of...
5
2616
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 form from a record set. Very simply it lists items that can be ordered by the customer. The customer simply enters the required qty for each item and hits submit:
2
257
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 Javascript? TIA
9
4184
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 honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation (e.g. this is numeric, this is alpha-numeric, etc.)
7
7002
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 is correct I need to submit to another page that uses the form data. I first tried making the form submit action= field point to the same file. When the form was correct, I tried loading the next page by using <META http-equiv refresh>. But...
11
3004
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 certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
8
2782
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 static html templates and the form input is checked using a validation class. Basically each form field is checked, every error is stored to an array and at the end of checking of the complete form, the array is output neatly at the top of the form. ...
7
3625
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
2981
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 background-color of the form element in question. I would like for example, if the user has forgotten to enter their email address, the field is highlighted so they know what they have to enter. Any valid fields are left the same color. here is...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10639
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.