Try to build a custom script " failed "

Can somebody help me to build a custom script to validate
my full_name field of my doctype Contact and my phone_number field of my doctype Telephone?

here the details:
the child table is Telephone with a phone_number field and
parent table is Contact with a full_name field.

below is my try to code my script
validation of my phone_number field but it isn’t working.

frappe.ui.form.on("Telefono", "validate", function(frm) {

var value = frm.doc.phone_number
var pattern = new RegExp("(^[0-9]{3} [0-9]{3} [0-9]{4})$");
var result = pattern.test(value);

   if (pattern.test(value)==false) {

       validated = false;


Any help I appreciate it.

What is the error you are getting? Is your event not firing or regex not working?

Hi, @Emmanuel_Ortega1! What are you trying to do with the script? Are you trying to check the phone numbers entered inside the childtable at the moment that they are entered? If yes, I think the script should be as follows:

frappe.ui.form.on("Telephone", "phone_number", function(frm, cdt, cdn){
var value = locals[cdt][cdn].phone_number;
var pattern = new RegExp("(^[0-9]{3} [0-9]{3} [0-9]{4})$");
var result = pattern.test(value);
//The rest of your code. :)

Hope it helps.

the code is working but I have to edit in full page to take effect.

excellent answer even it worked.
but instead of " var value = locals[cdt][cdn].phone_number; "
I used " var value = frappe.model.get_value(‘Contacto’, , ‘full_name’);" to get the value and validate the format.

1 Like

Glad to hear it! Congrats and happy coding! :smiley:

1 Like