How can I set allowed characters in a custom field?
In particular I’m trying to set two fields:
- field 1: only uppercase letters, numbers and spaces.
- field 2: only letters and numbers
How can I set allowed characters in a custom field?
In particular I’m trying to set two fields:
Hello @frehu01,
Custom fields are also fields under a DocType. Just like any other fields, you can use put up its validation method. You can hook it up using Custom Script
or Doc Events
for the Custom Field
.
Regards,
Ivan
Thanks @iRaySpace!
I’m not an programmer but I still like to share the solution I came up with. Maybe it helps others. I realized that it only works in the normal view, it doesn’t work in the quick entry view. So if somebody has a better solution: bring it on!
First one is for a custom field named suppliercode.
frappe.ui.form.on("Item", "validate", function(frm) {
var regex = /[^0-9A-Za-z]/g;
if (regex.test(frm.doc.suppliercode) === true){
frappe.msgprint(__("Suppliercode: Only letters and numbers are allowed."));
frappe.validated = false;
}
});
Next one is so that brands can only be entered in uppercase letters (including ÜÄÖÈÀÉ for French and German names), numbers and spaces.
frappe.ui.form.on("Brand", "validate", function(frm) {
var regex = /[^0-9A-ZÖÜÄÈÀÉ\s]/g;
if (regex.test(frm.doc.brand) === true){
frappe.msgprint(__("Brand: Only uppercase letters, numbers and spaces are allowed."));
frappe.validated = false;
}
});
My resources:
https://docs.erpnext.com/docs/user/manual/en/customize-erpnext/custom-scripts