Restrict field number limit

Hi, everyone.
I added a custom field in my Employee doctype called “Aadhaar Number” with a word limit set to 12.

Now, I want the field to accept only numerical values and to save only if the number is exactly 12 digits long. If the number is less than 12 digits, it should not allow saving. How can I achieve this?
Capture

Thank you

Issue sorted out with custom script.

frappe.ui.form.on('Employee', {
    validate: function(frm) {
       let aadhaarNumber = frm.doc.
  
    // Check if Aadhaar Number is exactly 12 digits long
    if (!/^\d{12}$/.test(aadhaarNumber)) {
        frappe.msgprint(__('Aadhaar Number must be exactly 12 digits long.'));
        frappe.validated = false;
    }
}
});

Reference already there :wink: :

You can find it in the forum with a short keyword.

1 Like

@NCP Thank you , i want to add esic field which has 17 digits and UAN no. which have 12 digits , is it possible to add all in this one script.

Yes @rs115199789,

Please check it.

frappe.ui.form.on('Your Doctype', {
    validate: function (frm) {
        var aadhar_number = frm.doc.aadhar_number;
        var esic_number = frm.doc.esic_number;
        var uan_number = frm.doc.uan_number;

        if (aadhar_number && aadhar_number.length !== 12) {
            frappe.msgprint(__('Aadhar card number must be 12 digits.'));
            frappe.validated = false;
        }

        if (esic_number && esic_number.length !== 17) {
            frappe.msgprint(__('ESIC number must be 17 digits.'));
            frappe.validated = false;
        }

        if (uan_number && uan_number.length !== 12) {
            frappe.msgprint(__('UAN number must be 12 digits.'));
            frappe.validated = false;
        }
    }
});

Please set your doctype name and field name in script.

1 Like