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("");
};
} })
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.
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
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'.
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);
};
}
})
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')">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With