Solved

changing input field background color on jquery validation

Posted on 2008-10-10
3
7,500 Views
Last Modified: 2012-05-05
I am implementing jquery form validation and would like to have the input field background color changed depending on the validation status. (red on error and green on success)

I got to a point where the background color changes on error but the input field becomes... 'ugly' looking one. How do I change only the background color?

Also is there a way to change the background color / message when the input entry is valid or successful?
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<style type="text/css">
#commentForm label.error {
	font-size:11px;
	color:red;
}
label.valid {
	background: url('/js/jquery/img/valid.gif') no-repeat;
	height:21px;
	width:21px;
	display:block;
	padding-left: 21px;
}
input.error, input select.error {
	background-color: red;
}
</style>
<script>
  $(document).ready(function(){
    $("#commentForm").validate({
		rules: {
			cemail: {
				required: true,
				email: true
			}
		},
		messages: {
			cemail: {
				required: "<img src='/img/invalid.gif' style='vertical-align: bottom; margin-left: 5px;'><br>Please enter the Email address",
				email: "<img src='/invalid.gif' style='vertical-align: top; margin-top:1px; margin-left: 5px;'><br>Invalid Email address"
			}
		},
		submitHandler: function() { alert("Submitted!") }
	});
  });
</script>
</head>
<body>
<form id="commentForm" name="commentForm" method="get" action="">
  <fieldset>
  <p>
    <label for="cemail">E-Mail</label>
    <em>*</em>
    <input id="cemail" name="cemail" size="25" />
  </p>
  </fieldset>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:eccen
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22687731
This script based on YUI seems to be closer to what you want.  You can do it in jQuery, but it will probably be more work.  You will probably want to redo some of the validation script.
http://blog.jc21.com/staging/jsvalidate_yui.php
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 22793968
In case of an error in the validation, the class error is applied to the form element(s).
You can define an .error rule in your css.
Specify background-color in this rule.

There's no rule applied in case of succesfull validation.
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 22793972
Oops, saw that you allready applied it... Sorry
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Easy responsive table out of existing table 28 55
Mobile page Scrolling down when refreshing 4 25
Jquery keyup 4 17
Center div containing a script 6 14
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now