Request for Server Script to Fix Mobile Number Field from Data to Phone Type

Dear Team,

As mentioned earlier, we are facing an issue with the “Mobile Number” field in the Customer Doctype. The field is currently of type “Data”, which allows users to enter invalid or incomplete phone numbers.

Although I have implemented a workaround by adding a custom Phone field and linking it to the Contact Doctype, we are still encountering issues with its visibility in Quick View and POS Search.

I am reaching out to see if anyone has experience writing a Server Script that could help resolve this issue by making the “Mobile Number” field function as if it were of type Phone. This would enforce proper validation and formatting for phone numbers, ensuring consistent and accurate data.

If anyone has any suggestions or can provide a Server Script solution, it would be greatly appreciated.

Looking forward to your feedback and solutions!

Best regards,

You can customize a form instead of using server scripts.

To solve this particular issue, you can customize the field to have proper validations. See these references to get an idea.

Ideally, raise a pull request upstream if you think it is a bug or can be improved for everyone.

Thank you for your suggestions. However, I would like to clarify that the issue we are facing is that the “Mobile Number” field is not allowing us to change its field type from “Data” to “Phone”. This limitation is why I was considering writing a Server Script to handle the validation and ensure proper functionality.

Additionally, I do believe this issue is something that could benefit everyone, as it addresses a core problem. Ideally, the “Mobile Number” field should be of type “Phone” and not “Data”, as it is crucial for proper validation and formatting of phone numbers. This is a fundamental requirement for the system to maintain the integrity of customer contact data.

I would appreciate any further guidance or solutions to resolve this issue, especially in terms of the field type change.

How to add it in ERPNext:

  1. Go to Customization → Custom Script

  2. Click New

  3. Set Document TypeCustomer

  4. Set Script TypeClient

  5. Paste the script content and Save

// ─────────────────────────────────────────────────────────────────//  ERPNext Custom Script — Customer Doctype//  Field  : mobile_no//  Purpose: Validate that Mobile Number is exactly 10 digits//           (digits only, no spaces / special characters)// ─────────────────────────────────────────────────────────────────

frappe.ui.form.on(“Customer”, {

// ── Triggered when the user leaves the mobile_no field ──────
mobile_no: function (frm) {
    validate_mobile_number(frm);
},

// ── Triggered on every Save attempt ─────────────────────────
validate: function (frm) {
    validate_mobile_number(frm);
}

});

/**

Validates that mobile_no contains exactly 10 digits.



Rules enforced:

Field must not be empty

Must be exactly 10 characters long

Must contain digits only (0-9); no spaces, dashes, or +country code



@param {object} frm - The current form object*/function validate_mobile_number(frm) {

const mobile = (frm.doc.mobile_no || “”).trim();

// Allow blank while the user is still filling the form;// the validate hook will catch it on save if required.if (!mobile) {return;}

const DIGITS_ONLY   = /^\d+$/;          // no letters / symbolsconst EXACTLY_TEN   = mobile.length === 10;const ALL_DIGITS    = DIGITS_ONLY.test(mobile);

if (!ALL_DIGITS) {frappe.throw(__("Mobile Number must contain digits only (0-9). "

“Please remove spaces, dashes, or country codes.”));// Highlight the field redfrm.set_df_property(“mobile_no”, “reqd”, 1);} else if (!EXACTLY_TEN) {frappe.throw(__("Mobile Number must be exactly 10 digits. "

“You entered {0} digit(s).”, [mobile.length]));} else {// Valid — clear any previous error highlightfrm.set_value(“mobile_no”, mobile);   // persist trimmed valuefrappe.show_alert({message : __(“Mobile Number looks good ✓”),indicator: “green”}, 3);}}