473,804 Members | 3,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

highlight text box when validation fails using php

pradeepjain
563 Contributor
hii. i am developing a new form for which validation is done using php , so the error text gets displayed also,I wanted to make a improvement for form. i.e can the textfield for which validation field be highlighted with say border:red; using php or javascript needs to be used. i am asking this bcos i am not using javascript for validation purpose in this form.
Aug 31 '09 #1
7 11342
zorgi
431 Recognized Expert Contributor
Maybe you could simply change the style/class of the input element. For example:

Expand|Select|Wrap|Line Numbers
  1. /*Some css*/
  2. <style>
  3. .errorClass{
  4.  border: solid 1px red;
  5. }
  6. .defaultClass{
  7. /*defaults*/
  8. }
  9. </style>
  10.  
  11. <?php
  12. if($error){
  13.  $class = "errorClass";
  14. }else{
  15.  $class = "defaultClass";
  16. }
  17. ?>
  18.  
  19. <input type="text" class="<?php echo $class?>">
  20.  
Aug 31 '09 #2
pradeepjain
563 Contributor
But how will it recognize the particular field for which error has occurred! Must it be repeated for all the fields?
Aug 31 '09 #3
zorgi
431 Recognized Expert Contributor
It would be easier if we could see your code.
Aug 31 '09 #4
pradeepjain
563 Contributor
Expand|Select|Wrap|Line Numbers
  1.  <script type="text/javascript" src="/sample/datetimepicker_css.js"></script>
  2. <h2 class="bar"></h2>
  3. <br/>
  4. <?php
  5. include_once('valid-scripts/validateData.php');
  6. function showForm($error=false){
  7. $errors='';
  8. if($error['name']==TRUE){
  9. $errors.="<li>Error  in name</li>";
  10. }
  11.  
  12. if($error['address']==TRUE){
  13. $errors.="<li>Error  in Address</li>";
  14. }
  15.  
  16.  
  17. if($errors){
  18. $class = "valid-error";
  19. ?>
  20. <div id="errors">
  21. <ul>
  22. <?php echo $errors; ?>
  23. </ul>
  24. </div>
  25. <?php } 
  26. ?>
  27. <form id="new" name="patient_record" method="POST" action=<?php $_SERVER['php_self'] ?>>
  28. <fieldset id="personal">
  29. <legend>PERSONAL INFORMATION</legend>
  30. <label for="Name">Name : </label>
  31. <input type="text" size="30"  id="name" class="<?php echo $class?>" name="name"  value="<?php echo $_POST['name'] ?>"onkeypress="javascript:indic_script_lang(event);" onkeydown="javascript:toggleKBMode(event);" onfocus="javascript:indic_script_show_typing_method();" onblur="javascript:indic_script_hide_typing_method();"><span class="star">*</span><span class="star">
  32. <br/>
  33. <label for="Address">Address : </label>
  34. <textarea name="address"  onkeypress="javascript:indic_script_lang(event);" onkeydown="javascript:toggleKBMode(event);" onfocus="javascript:indic_script_show_typing_method();" onblur="javascript:indic_script_hide_typing_method();"><?php echo $_POST['address'] ?> </textarea><span class="star">*</span>
  35. </fieldset>
  36.  
  37. <p>
  38.   <input id="button1" type="submit" name='submit' value="Submit Record" /> 
  39. </p>
  40. </form>
  41. <?php
  42. }
  43. if (!isset($_POST['submit'])) {
  44.  
  45. showForm();
  46. } else {
  47. $_POST    = snipExtras($_POST);
  48. $error=array();
  49. if(!validate_name($_POST['name'])) $error['name']=true;
  50. if(!validate_address($_POST['address'])) $error['address']=true;
  51. if($error){
  52. showForm($error);
  53. } else {
  54. echo 'Submission was success!';
  55. }
  56. }
  57. ?>

here you go
Aug 31 '09 #5
zorgi
431 Recognized Expert Contributor
Ok

First make your CSS:
Expand|Select|Wrap|Line Numbers
  1. <style>
  2. .error{
  3.     border: 1px solid red;    
  4. }
  5. </style>
  6.  
Than do this for name input:

Expand|Select|Wrap|Line Numbers
  1. if($error['name']==TRUE){
  2.     $errors.="<li>Error  in name</li>";
  3.     $name_class = "class='error'"; //this is added
  4. }
  5.  
Than for your input element do this:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="name" <?php echo $name_class;?> />
  2.  
I am sure this can be done differently but considering code you produced here this is probably the quickest way. Hope this helps.
Aug 31 '09 #6
pradeepjain
563 Contributor
Can you tell the other way of doing it!!i want to make the code standardised! I am ready to re modify the code!
Sep 1 '09 #7
sonofjorel
1 New Member
I have a form with PHP Validation and this is EXACTLY the solution I've been looking for! Thank you SO MUCH!
For my form I had to create variables for each required field:
Expand|Select|Wrap|Line Numbers
  1. $name_class = "class='error'";
  2. $email_class = "class='error'";
  3. $msg_class = "class='error'";
as well as create rules to return each field to its "valid" default CSS state and so it is no longer highlighted in red when the validation conditions are TRUE. Also, don't forget to name these variable at the top of your PHP code so the form loads with the default CSS rules...
Expand|Select|Wrap|Line Numbers
  1. $name_class = "class='default'";
  2. $email_class = "class='default'";
  3. $msg_class = "class='default'";
Oct 24 '12 #8

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

Similar topics

13
4538
by: David Morgan | last post by:
Hello I have a little function to highlight text if it exists. Function Highlight(vFind, vSearch) Dim RegEx Set RegEx = New RegExp RegEx.Pattern = vFind RegEx.IgnoreCase = True Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
5
3871
by: Merx | last post by:
Hi! Is it possible, using Javascript, to highlight (in yellow..) all the occurrences of "number: +" (regex) of the document? Merx
5
2614
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:
1
5337
by: john woo | last post by:
Hi I'm not good at JS, but want to get more about it. I want to use a JSP (the java code just used to get date, the rest are html and javascript), to display a table. the requirement is the all rows in even number in light-blue, rows in odd number in light gray, when a mouse clicks on a row, this row gets highlighted and in yellow, and it keeps highlighted
0
5425
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this board. I have a form that contains a ListView and a GroupBox control. The GroupBox control itself contains several TextBox Controls. I have Validating and Validated event handlers set for the GroupBox control, and a SelectedIndexChanged event...
5
10924
by: Atara | last post by:
I am trying to convert the following code to VB .Net, I still have some gaps (the lines that are marked with (*)) and also I need an ending condition for the while loop. any help would be appreciated. Thanks. Atara. ------------------------- Original code:
1
1625
by: Joel Barsotti | last post by:
Is there anything builtin to ASP.net that allows you to tie a text box to a button so when you press enter in the text box it emulates clicking a near by button. I've coded up some client side javascript that does this, but it fails when their is a validator monitoring the text box (in firefox, but that's 10% of my visitors).
2
2928
by: Celeste | last post by:
Hello, I'm trying to parse the referring url for google search terms so that when this page loads it will scroll to and highlight the search term(s). Should i be using document.referrer? Please take a look at my code and tell me what i'm doing wrong: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <title>highlight</title>
4
8243
by: HenrikL | last post by:
Hi. I have a textbox that have a binding on it with converter and validationrules. When a validation error ouccur the foreground of the textbox will change to red cause it has a style.triggers that are connect to it and it works as it should.. What I wanna to do it in some special case I wan to test tb.Text.Equals("myTestStr") or if the text lenght too large etc.. and I wanna same behave as a validation has error on the textbox if the...
0
9708
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
10589
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
10340
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
10327
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,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7625
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
6857
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
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.