Mingg Lex
Mingg Lex

Reputation: 151

How to add validation to fields in Odoo?

I have to add validation in the signup form for Odoo (which is the auth_signup_login_templates.xml file in the auth_signup module. I need to ensure that the Name contains on alphabets and are within 3 to 15 characters. Right now, by default, the code for the name is:

         <div class="form-group field-name">
            <label for="name" class="control-label">Your Name</label>
            <input type="text" name="name" t-att-value="name" id="name" class="form-control" placeholder="e.g. John Doe"
                required="required" t-att-readonly="'readonly' if only_passwords else None"
                t-att-autofocus="'autofocus' if login and not only_passwords else None" />
        </div>

The xml page can be found here

Upvotes: 0

Views: 2992

Answers (1)

Hilar AK
Hilar AK

Reputation: 1675

For validations we prefer Javascript/Jquery :) eg:

$('#name').keypress(function (e) {
    var regex = new RegExp(/^[a-zA-Z\s]+$/);
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
        return true;
    }
    else {
        e.preventDefault();
        return false;
    }
});

but for min and max length, you can use data attributes. eg:

<input data-rule-minlength="3" data-rule-maxlength="8" data-msg-minlength="Exactly 3 characters please" data-msg-maxlength="Exactly 8 characters please">

Upvotes: 1

Related Questions