2021-02-11 09:38 AM
Hello,
Does anyone have an existing custom object code for force a ph# format?
We need the users to enter phone number like this???-???-????. We wont let them save the record unless they use that format.
If you have code for this we would appreciate it.
Thank you
2021-03-09 09:14 AM
Thanks IIya .Should I make a change to the code of the custom object?
2021-03-09 09:17 AM
Yes, in this part.
It tries to get length from NULL, I believe. However, the question still, why it returns null, when it supposed to be a value.
Maybe getFieldId is not correct.
f(Number($CM.getFieldValue(getFieldId).length)<10 && Number($CM.getFieldValue(getFieldId).length)> 1){
WarningAlert('The phone number entered (' + fieldName + ') is not in the proper format of (###-###-####)',"Phone Number Error");
} else {
ShowAnimationAndPostback('master$btn' + action);
}
2021-03-09 09:28 AM - edited 2021-03-09 09:32 AM
The formula worked before when it just checked the phone number format.
Then I asked @DavidPetty to only check format when editing that field.
If we edit another field we don't want to force phone number format. We want to be able to save the record.
Only check format if that field is changing.
Let me know if there is any recommended code changes. Thank you
2021-03-09 10:36 AM
Rod, I made some changes to check to see if the field is null first. See how this version works.
<script type="text/javascript">
var fieldName = 'Phone Number';
// Hijack Save Button
$('#master_btnApply1, #master_btnApplyIcon').unbind('click').prop("onclick", null).click(function(){ checkPhoneNumber('Apply');return false;})
// Clone Save And Close Button
$('#master_btnSave1').unbind('click').prop("onclick", null).click(function(){ checkPhoneNumber('Save')});
// Hide Right-Click Save and Save and Close Right-Click Menu Items
$('.rmLink:Contains("Save")').parent().parent().hide();
$('.rmLink:Contains("Save and Close")').parent().parent().hide();
function checkPhoneNumber(action){
var getFieldId = lookupFieldId(fieldName);
if ($CM.getFieldValue(getFieldId) != null){
if(Number($CM.getFieldValue(getFieldId).length)<10){
WarningAlert('The phone number entered (' + fieldName + ') is not in the proper format of (###-###-####)',"Phone Number Error");
} else {
ShowAnimationAndPostback('master$btn' + action);
}
} else {
ShowAnimationAndPostback('master$btn' + action);
}
}
function lookupFieldId(fldName){
var goFindId = null;
var r = /^a$/;
try{
$('.FieldLabel').each(function(){
if(($(this).text().indexOf(fldName + ':') != -1) && ($(this).text().indexOf(fldName + ':') == 0)){
goFindId = $(this).find("span")[0].id;
return false;
}
});
} catch (err) {console.log(err)}
try {if (!goFindId) goFindId = $('.SectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
return goFindId ? $LM._layoutItems[goFindId.replace( /^\D+/g, '')].fieldId : 0;
}
$.expr[':'].findField = function(a, i, m) {
return $(a).text().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_').match("^" + m[3].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_') + "$");
};
</script>
Advisory Consultant
2021-03-09 12:05 PM - edited 2021-03-09 12:21 PM
Good news bad news.
I was able to edit another field and save.
The problem is took away the phone number format force.
here is the console
JQMIGRATE: Migrate is installed, version 3.0.1
2021-03-09 12:37 PM - edited 2021-03-09 12:37 PM
Rod, did you update the field name in the custom object? I had no issues testing with no number populated or with the incorrect format.
Advisory Consultant
2021-03-09 01:19 PM
Console error.
SCRIPT1004: Expected ';'
Record.aspx (611,121)
You can see here a saved record. Invalid format.
Here is latest code. I changed the phone number name.
<script type="text/javascript">
var fieldName = 'Customer Contact Number';
// Hijack Save Button
$('#master_btnApply1, #master_btnApplyIcon').unbind('click').prop("onclick", null).click(function(){ checkCustomer Contact Number('Apply');return false;})
// Clone Save And Close Button
$('#master_btnSave1').unbind('click').prop("onclick", null).click(function(){ Customer Contact Number('Save')});
// Hide Right-Click Save and Save and Close Right-Click Menu Items
$('.rmLink:Contains("Save")').parent().parent().hide();
$('.rmLink:Contains("Save and Close")').parent().parent().hide();
function checkPhoneNumber(action){
var getFieldId = lookupFieldId(fieldName);
if ($CM.getFieldValue(getFieldId) != null){
if(Number($CM.getFieldValue(getFieldId).length)<10){
WarningAlert('The phone number entered (' + fieldName + ') is not in the proper format of (###-###-####)',"Phone Number Error");
} else {
ShowAnimationAndPostback('master$btn' + action);
}
} else {
ShowAnimationAndPostback('master$btn' + action);
}
}
function lookupFieldId(fldName){
var goFindId = null;
var r = /^a$/;
try{
$('.FieldLabel').each(function(){
if(($(this).text().indexOf(fldName + ':') != -1) && ($(this).text().indexOf(fldName + ':') == 0)){
goFindId = $(this).find("span")[0].id;
return false;
}
});
} catch (err) {console.log(err)}
try {if (!goFindId) goFindId = $('.SectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
try {if (!goFindId) goFindId = $('.SubSectionLabel:findField("' + fldName + '")')[0].id;} catch (err) {}
return goFindId ? $LM._layoutItems[goFindId.replace( /^\D+/g, '')].fieldId : 0;
}
$.expr[':'].findField = function(a, i, m) {
return $(a).text().replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_').match("^" + m[3].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_') + "$");
};
</script>
2021-03-09 01:33 PM
If you click on the "Record.aspx (611,121)' link it will take you to the line where the error is. Send me a screenshot of that because I don't see any syntax errors on my version.
Advisory Consultant
2021-03-09 01:39 PM
Also remember we are on 6.6 p6
If you have access to hAetna dev you can see it too.