Caleb
Caleb

Reputation: 825

How can I display an error message via JavaScript?

I'm creating a form, and I'm validating it with JavaScript. The way it works now, is if a value is not correct, it displays an alert stating that you need to enter text. My question is, how can I change it so that instead of an alert, the text box will turn red, and something like: "This is required" will show next to the textbox. I think I can solve this with jQuery, but I was wondering if anyone had any suggestions. THANKS!

P.S. If you go to yahoo.com, click Sign Up, and click Create Account (without entering anything), you'll see what I'm aiming at.

Upvotes: 0

Views: 2889

Answers (2)

Clyde Lobo
Clyde Lobo

Reputation: 9174

Well , I would use a jQuery Validation plugin which is at

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

You just need to add the css class "required" to the elements that you need to be validated

Upvotes: 1

Philippe Leybaert
Philippe Leybaert

Reputation: 171914

You could create a CSS class with the following properties:

input.ERROR { border-color: red; }

Using jQuery, you can add this class to the input box with the incorrect value:

$("input[name=Address]").addClass("ERROR");

(assuming the input box with the name "Address" had the wrong value)

Upvotes: 4

Related Questions