Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default "please fill out this field" in two field

Tags:

html

I would like two different messages in two field, for example, the username and password field that contain messages like "username cannot be blank" and "password cannot be blank". I only managed to change the message, but it is then the same for both fields. It's here

$(document).ready(function() {
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++) {
    elements[i].oninvalid = function(e) {
        e.target.setCustomValidity("");
        if (!e.target.validity.valid) {
            e.target.setCustomValidity("Username cannot be blank");
        }
    };
    elements[i].oninput = function(e) {
        e.target.setCustomValidity("");
    };
} })
like image 483
Godfather Avatar asked Jun 02 '13 19:06

Godfather


People also ask

How do I set the default value of a field?

Setting value with default_get function In this method, we can use a function default_get function. This function is used to set the values for multiple fields.

How to set default values to fields in odoo15?

In this blog, we will be discussing how to set default values to fields in odoo15. In Odoo, we can set a default value for a field during the creation of a record for a model. We have many methods in odoo for setting a default value to the field. The main methods are, 1. Passing Values Through Kwargs 1. Passing Values Through Kwargs

What is the default value of the 'state' field?

In this, 'state' field is a selection field and the default value of the 'state' field is set as draft i.e, Quotation. The following screenshot shows that if the user creates a new sale order, then the default 'state' is draft, i.e., 'Quotation'.


2 Answers

sorry, some mistakes in my code. Try that, that works for me :

$(document).ready(function() {
var msg="";
var elements = document.getElementsByTagName("INPUT");

for (var i = 0; i < elements.length; i++) {
   elements[i].oninvalid =function(e) {
        if (!e.target.validity.valid) {
        switch(e.target.id){
            case 'password' : 
            e.target.setCustomValidity("Bad password");break;
            case 'username' : 
            e.target.setCustomValidity("Username cannot be blank");break;
        default : e.target.setCustomValidity("");break;

        }
       }
    };
   elements[i].oninput = function(e) {
        e.target.setCustomValidity(msg);
    };
} 
})
like image 101
scraaappy Avatar answered Nov 01 '22 08:11

scraaappy


You can use this code.

   <input type="text"  required="" name="username" placeholder="Username" oninvalid="this.setCustomValidity('Username cannot be blank')">
   <input type="password"  required="" name="password" placeholder="Password" oninvalid="this.setCustomValidity('Password cannot be blank')">
like image 23
Obaidul Haque Avatar answered Nov 01 '22 09:11

Obaidul Haque