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

Here is console. Also you can see the ph# is not formatted properly.

console.png

Ah, I see the issue.  The initial custom object was for 6.8+ and the id of the Save and Save and Close buttons are different.  I've updated the custom object below for the version you're on.

<script type="text/javascript">
     var fieldName = 'Customer Contact Number';

     // Hijack Save Button
     $('#master_btnApply').unbind('click').prop("onclick", null).click(function(){ checkPhoneNumber('Apply');return false;})
     
     // Clone Save And Close Button
     $('#master_btnSave').unbind('click').prop("onclick", null).click(function(){ checkPhoneNumber('Save')});

     function checkPhoneNumber(action){
        var getFieldId = lookupFieldId(fieldName);
        if(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);
        }
     }
     
     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

Sorry we are in the dinosaurs here at Aetna. Thank you for your patience and getting this to work for us.

Thank you.

Hey David,

 

I don't think this works exactly the way I envisioned. I need it to only force phone number format when we are editing that field.

Now we cant edit any field if the phone number format isn't correct.

 

 

Can you recommend anything to add to the code to only force formatting when someone is editing the field.

 

Rod, try replaceing this line

if(Number($CM.getFieldValue(getFieldId).length)<10){

with

if(Number($CM.getFieldValue(getFieldId).length)<10 && Number($CM.getFieldValue(getFieldId).length)> 1){
 

 Advisory Consultant

Thanks but that didn't work.  The record did not have a phone number. I tried to edit another field but the record wont save.

The save and save and close buttons are there but they wont click. Here is the latest code after I made your change.

Please remember I am on an old version of Archer.

 

<script type="text/javascript">
     var fieldName = 'Customer Contact Number';

     // Hijack Save Button
     $('#master_btnApply').unbind('click').prop("onclick", null).click(function(){ checkPhoneNumber('Apply');return false;})
    
     // Clone Save And Close Button
     $('#master_btnSave').unbind('click').prop("onclick", null).click(function(){ checkPhoneNumber('Save')});

     function checkPhoneNumber(action){
        var getFieldId = lookupFieldId(fieldName);
         if(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);
        }
     }
    
     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>

@DavidPetty 

I think you are close, Please look at my code above.

The save and save close buttons are there, but you cant click them.

 


Thank you

Rod, any errors in the browsers console when you edit the record?

 Advisory Consultant

Sorry should have included that.

 

SCRIPT5007: Unable to get property 'length' of undefined or null reference

Check that the expression $CM.getFieldValue(getFieldId) returns NULL in the IF condition.