I want to enforce a specific phone number format (e.g., (+91) 123 4567) in the Contacts module before saving. I prefer using JavaScript validation in EditView to ensure users enter the correct format.
Iβd appreciate any guidance.
I want to enforce a specific phone number format (e.g., (+91) 123 4567) in the Contacts module before saving. I prefer using JavaScript validation in EditView to ensure users enter the correct format.
Iβd appreciate any guidance.
You can write a simple javascript code.
Create the file at the below location.
/custom/modules/Contacts/js/phone_format.js
I have created the custom JS file at the specified location, and it is loading, but itβs not applying the expected changes.
Hello, I think you can allow users to enter a valid phone number without the country code and check if the phone is valid 10 digit value and then add the country code by auto correcting the format. You can create an entry point which will perform this validation and return the formatted value. in the javascript you just take care of highlighting the error and stop the save action. If possible I will share code after some time. Thanks
Please check this if it helps
<?php
$entry_point_registry['FormatPhone'] = array(
'file' => 'custom/entrypoints/format_phone_number.php',
'auth' => true,
);
<?php
if(!defined('sugarEntry') || !sugarEntry){
die('Not A Valid Entry Point');
}
$phone = urldecode($_REQUEST['phone']);
$phone = preg_replace('/[+,(,),\s]/', '', $phone);
if(strlen($phone) < 10 || strlen($phone) > 10){
echo json_encode(array('error'=>'only 10 digits are allowed in the phone value'));
}else{
$re = '/(\d{3})(\d{3})(\d{4})$/';
$result = preg_replace($re, '(+91) $1 $2 $3', $phone);
echo json_encode(array('output'=>substr($result,0)));
}
$(document).ready(function (){
$('.phone').change(function(){
let phoneInput = $(this);
if($("#checkingDV").length){
$("#checkingDV").remove();
}
const checkingDv = document.createElement("div");
checkingDv.id="checkingDV";
checkingDv.innerHTML="<span id=\"checkingDvSpan\">checking...</span>";
phoneInput.parent().append(checkingDv);
let callback = {
success: function(result){
let formatted_phone = JSON.parse(result.responseText);
if($.trim(formatted_phone.error).length>0){
$("#checkingDvSpan").css({"color":"#FF0000"});
$("#checkingDvSpan").text('Invalid Value: '+formatted_phone.error);
let validationResult = false;
removeFromValidate("EditView", phoneInput.attr('id'));
$('.required.validation-message').remove();
addToValidateCallback("EditView", phoneInput.attr('id'), "varchar", false, formatted_phone.error, function(a,b){
checkingDv.remove();
return validationResult;
});
}
else{
checkingDv.remove();
$('.required.validation-message').remove();
removeFromValidate("EditView", phoneInput.attr('id'));
phoneInput.val(formatted_phone.output);
}
},
error: function(error){
alert('something went wrong!');
}
};
YAHOO.util.Connect.asyncRequest("GET","index.php?entryPoint=FormatPhone&phone="+phoneInput.val(),callback);
});
});
Quick Repair & Rebuild
Reload the page
Please make changes as per your requirement.
Thank you for the help, Iβll try this approach