Important Update: Some Community URL Redirects are Under Maintenance. 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

DavidPetty
Archer Employee
Archer Employee

Rod, see how below works for you.

 

Update line 2 with the correct field name and line 17 is the text for the warning message to the user.

<script type="text/javascript">
     var fieldName = 'Text (Calculated)';
     
     // 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(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

I do not think I did it correctly. The bold in line two is the actual field name.

I dont get any error when I save the record.

 

Thank you so much for the help.

 

 

<script type="text/javascript">
var Customer Contact Number = 'Text (Calculated)';

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

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>

Close and my bad for not being clearer.

 

It should be:

var fieldName = 'Customer Contact Number';‍‍

 

 Advisory Consultant

No this is all my fault. I don't know the coding part of Archer. Do I have the custom object in the correct spot? It still doesn't work.
I put it under the field I need to format.

 

phone number format.png

No worries

 

The placement doesn't matter.  You should get a warning prompt when trying to save a record that doesn't have a complete phone number entered.  The custom object is looking to see if the phone number entered is the correct length of 10 numbers.

 Advisory Consultant

Here is the latest version. Do I have the fiedName in the correct spot? Object isnt working. Thank you

 

<script type="text/javascript">
var fieldName = 'Customer Contact 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(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);
}
}

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>

The variable looks fine.

 

Make sure the custom object display is configured for Edit Mode.  Also, see if any errors are thrown by open the browser developer tools and look at the console when you edit the record and click on the Save button.

 

2021-02-11_13-48-31.gif

 Advisory Consultant

Here is what I get in dev tools.

 

<script type="text/javascript">
//<![CDATA[
window.__TsmHiddenField = $get('scriptManager_TSM');//]]>
</script>
</form>
</body>
</html>

Hmm, that's not from the custom object.

 

Is it listed as an error?

 

Can you post a screenshot of the console?

 Advisory Consultant