Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Need custom object code. Force Phone # format

RodEhrlich
Contributor III

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

28 REPLIES 28

RodEhrlich
Contributor III

Thanks IIya .Should I make a change to the code of the custom object?

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);
        }

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

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

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

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

Console error.

SCRIPT1004: Expected ';'

Record.aspx (611,121)

 

You can see here a saved record. Invalid format.

Customer Contact Number: 7765

 

Other field successfully field saved.

3/9/2021 1:09:22 PM Ehrlich, Rod K Field Change NextGen (MNG) PSUID Updated: 67865786 67

 

 

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>

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

Also remember we are on 6.6 p6

 

If you have access to hAetna dev you can see it too.

 

customer contact number error.png