Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting invalid form inputs

I have been working on creating a form with a set of fields like username, passwords etc.... I want to make validation when the SUBMIT button is clicked.

I'm trying to get alert from my border color. All fields are valid my border must change into Green color If it has any errors it should change to red color.

Any one has any ideas regarding to my problem

If anyone has any suggestion??

like image 455
Vivek Dragon Avatar asked Oct 13 '12 11:10

Vivek Dragon


People also ask

How to validate HTML input field?

The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this attribute is set, the element matches the :required UI pseudo-class and the form won't submit, displaying an error message on submission when the input is empty.

What CSS selector would you use to create a style sheet rule for all invalid required form fields created with the input element?

This :invalid selector is used to select every form elements which does not validate according to the elements.

Which CSS class can be used to style an input that is invalid?

The :invalid selector allows you to select <input> elements that do not contain valid content, as determined by its type attribute. :invalid is defined in the CSS Selectors Level 3 spec as a “validity pseudo-selector”, meaning it is used to style interactive elements based on an evaluation of user input.

What is invalid CSS?

The :invalid CSS pseudo-class represents any <form> , <fieldset> , <input> or other <form> element whose contents fail to validate.


2 Answers

You can use jquery plugin.... here you are. JQuery Form validation custom example

like image 127
Younus Ali Avatar answered Sep 20 '22 13:09

Younus Ali


Use jQuery validation plugin: http://docs.jquery.com/Plugins/Validation

In this plugin, you have to define validation rules for the field. You can also set the error messages for given field for given validation rule.

This plugin adds classes to valid and invalid field.

You have to give the css for that class.

For example:

$(document).ready(function(){
        $(".my_form").validate({
                    rules:{  // validation rules
                            email_address: {
                                required:true,
                                email: true
                            },
                            password:{
                                minlength: 6
                            },
                            confirm_password: {
                                         equalTo:"#password"
                            }
                    },
                    messages: {
                        email_address: {
                           required: "Please enter email address",
                           email: "Please enter valid email address",
                       },
                       /*
                          likewise you can define messages for different field 
                          for different rule.
                       */
                    }
                    errorClass: "signup_error",  
                     /*This is error class that will be applied to 
                      invalid element. Use this class to style border. 
                      You can give any class name.*/
                });
      });

Once you click on submit button, and field is invalid, the plugin adds class to the element that you have specified as errorClass, and when you enter valid value in the field, the plugin will remove this class and will add 'valid' class by default.

You can use these two classes to style valid and invalid element using simple element.

.valid {
   border-color:"green"
}

.signup_error {
  border-color:"red"
}

Hope this resolves your problem.

like image 37
Kalpesh Patel Avatar answered Sep 18 '22 13:09

Kalpesh Patel