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());
}
});
},
});
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
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.