Child Table Client Script

I have child table Name “R Product” and child table have filed called “serial_number”, child table link with “R Main” doctype with field name “product”,

i don’t want contain spaces or special characters in “serial_number” field, so i have script, but it’s not working

frappe.ui.form.on('R Main', {
    onload: function(frm) {
        frm.set_query('serial_number', 'product', function(doc, cdt, cdn) {
            var value = locals[cdt][cdn].serial_number;

            if (/[ -\/>\]\{)\s]/.test(value)) {
                frappe.msgprint(__("Serial number cannot contain spaces or special characters (-/>]{)"));
                frappe.validated = false;
            } else {
                frm.set_value('serial_number', value.toUpperCase());
            }
        });
    },
});

Anyone have solution for this ?

Hi @nilpatel42,

Please try it.

frappe.ui.form.on('R Main',  {
    validate: function(frm) {
        $.each(frm.doc.r_product || [], function(i, d) {
            var regex = /[ -\/>\]\{)\s]/;
            var serialNumber = d.serial_number;
            if (!regex.test(serialNumber)) {
                frappe.throw(__("Serial Number should only contain alphanumeric characters in row {0}", [d.idx]));
                valid = false;
            }
        });
    }
});

Please configure the name of your document type, field name, and table field name as per the script.

Thank You!

1 Like

Thanks @NCP

above script is not working for me, but based on your script i tried with this new script

frappe.ui.form.on('R Main', {
    validate: function(frm) {
        let valid = true;

        $.each(frm.doc.r_product || [], function(i, d) {

            var regex = /^[a-zA-Z0-9]+$/;
            var serialNumber = d.serial_number;

            if (!regex.test(serialNumber)) {
                frappe.msgprint(__("Serial Number should only contain alphanumeric characters in row {0}", [d.idx]));
                valid = false;
                return false;  
            }
        });

        return valid;
    }
});

This script 50 % working,
problem is when serial number is not correct and click on save button its show popup and save doctype,
my requirement is employees are not able save doctype if serial number is not correct

any mistake ?

use frappe.throw and check it. you can also use the before_save.

frappe.ui.form.on('R Main', {
    before_save: function(frm) {
        $.each(frm.doc.r_product || [], function(i, d) {
            var regex = /^[a-zA-Z0-9]+$/;
            var serialNumber = d.serial_number;
            if (!regex.test(serialNumber)) {
                frappe.throw(__("Serial Number should only contain alphanumeric characters in row {0}", [d.idx]));
            }
        });
    }
});

Thank You!

1 Like

Thank You @NCP

:slightly_smiling_face: It’s working

also Bonus code for me
This new script is correct serial number when employee write wrong serial number

frappe.ui.form.on('R Main', {
    before_save: function(frm) {
        $.each(frm.doc.product || [], function(i, d) {
            var serialNumber = d.serial_number;
            if (serialNumber) {
                var cleanedSerialNumber = serialNumber.toUpperCase();
                cleanedSerialNumber = cleanedSerialNumber.replace(/[^A-Z0-9]/g, '');
                frm.doc.product[i].serial_number = cleanedSerialNumber;
            }
        });
    }
});

Posting this code for reference

1 Like

Thanks for sharing code for reference. It will help everyone.

I believe that $.each() is an async method. So form can be saved and transaction is already completed when frappe throwed an exception or you cleaned the serial number.

I suggest you to use a server script for that or test the client script accordingly.