How to validate phone field for only using number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ivan2805
    Junior Member
    • Jul 2022
    • 24

    How to validate phone field for only using number

    Hi everyone,

    I just notice that on the phone field inside the lead entity can be filled with anything including special character such as "-" and also letter.
    Is there any way that i can put some kind of rule inside the phone field that can only be filled with numbers.

    FYI, I'm not a programmer, so i will be appreciate if you can help me providing the steps to do this as clear as possible.

    Thank you in advance for the help
  • AgentT
    Member
    • Aug 2021
    • 77

    #2
    You can add validations to the phone number field from the phone.js file.

    You will want to customise it, so create your own phone.js under client/custom/src/views/fields/phone.js which should extend the parent phone.js like:
    Code:
    define('custom:views/fields/phone', 'views/fields/phone', function (Dep) {
        return Dep.extend({
    
        setup: function () {
            Dep.prototype.setup.call(this);
        },
    
        validatePhoneData: function () {
            <YOUR CODE HERE>
        }
        });
    }
    You can add regex validations on the phone number here.

    Then have the phone number fields in entities point to this file, so under entityDefs/Entity.json where you have the phoneNumber field, add the parameter "view": "custom:views/fields/phone".

    Cheers!

    Comment

    • ivan2805
      Junior Member
      • Jul 2022
      • 24

      #3
      Hi AgentT

      Thank you for your help,

      However since im not a programer, I may need to ask some simple question, because i really don't know

      On the code that you mention above, you put "<YOUR CODE HERE>" What kind of code that i need to put there.

      And second, where is the path to the entityDefs/Entity.json?

      Thank you

      Comment

      • AgentT
        Member
        • Aug 2021
        • 77

        #4
        My validatePhoneData looks like this:
        Code:
        validatePhoneData: function () {
          var data = this.model.get(this.dataFieldName);
        
          if (!data || !data.length) return;
          var numberList = [];
          var notValid = false;
        
          data.forEach(function (row, i) {
            var number = row.phoneNumber;
            // Add validation for the Phone number
            if (!(/^[\+]?[1]?[\s]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4}$/.test(number))) {
              var msg = this.translate('The phone format is wrong!').replace('{field}', this.getLabelText());
              this.showValidationMessage(msg);
              notValid = true;
              return true;
            }
        
            var numberClean = String(number).replace(/[\s\+]/g, '');
            if (~numberList.indexOf(numberClean)) {
               var msg = this.translate('fieldValueDuplicate', 'messages').replace('{field}', this.getLabelText());
               this.showValidationMessage(msg, 'div.phone-number-block:nth-child(' + (i + 1).toString() + ') input');
               notValid = true;
               return;
            }
            numberList.push(numberClean);
            }, this);
        
            if (notValid) {
              return true;
            }
        }
        This will validate my phone numbers to have only numbers and only certain patterns.

        The path to your custom entityDefs will be something like custom/Espo/Custom/Resources/metadata/entityDefs/Lead.json

        Comment

        Working...