473,406 Members | 2,259 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,406 software developers and data experts.

highlight text box when validation fails using php

pradeepjain
563 512MB
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 11308
zorgi
431 Expert 256MB
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 512MB
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 Expert 256MB
It would be easier if we could see your code.
Aug 31 '09 #4
pradeepjain
563 512MB
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 Expert 256MB
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 512MB
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
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
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 =...
5
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
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...
1
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...
0
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...
5
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...
1
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...
2
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? ...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.