Client or Server Script help to validate custom fields data

Hello All,

We need help to validate custom fields data by client or server script.

  1. Custom fields like Phone 1, Phone 2 and Phone 3 in Customer doctype.
  2. Need validate like all 3 phones should not be duplicate.
    e.g. >> customer 1 we added 12345 in phone 1
    >> customer 2 if we add 12345 in phone 2, it should validate and give message like 12345 is already exists in phone 1 of customer 1.

Thank you,

validate(frm){
if (frm.doc.phone_1){
if(frm.doc.phone_1== frm.doc.phone_2){
alert(“Phone 2 is Existing in Phone 1”);
frappe.validated=false;
}
}
}

Hi, Thank you for your reply.

We need to validate from database records if phone 1 = 123456 is already added in database and for new customer if we add phone 2 = 123456, it should check in database and show message.

image

I appreciate your help.

def validate_phone_number(phone_number):
    # Check if the phone number exists in the database for existing customers.
    existing_customer = frappe.get_all("Customer", filters={"mobile_no": phone_number}, fields=["name"])
    
    if existing_customer:
        # Phone number already exists for a customer.
        return _("Phone number {} is already associated with customer {}.").format(phone_number, existing_customer[0]["name"])
    ```
extend your logic and add it to before_save hook (Insert  + update ) will be covered

Thank you.