---
original_url: "https://jace.pro/blog/client-script-validating-phone/"
format: markdown
ai_optimized: true
---

Client Script - Validating Phone- # Client Script - Validating Phone

September 26, 2017 [client-scripts ](/tags/client-scripts/)[servicenow](/tags/servicenow/)

  Enable AI AnimationI’ve found two real ways to verify the data meets the needs you have.

Client script to do entire validation
- Client script to ask server if data is valid

This will use all client-side validation. I set up a config variable
where you need to;

- Specify the field to check
- Specify how long you need the number to be without non-digits
- Specify the error that will appear
- Specify how the number should be broken up

`function onChange(control, oldValue, newValue, isLoading) {
    var config = {
        field: 'phone',
        required_length: 10,
        error: 'Invalid telephone number.  Please enter your telephone number including area code.',
        sections: {
            one: 3,
            two: 3,
            three: 4
        }
    };
    // no need to change the below
    if (isLoading || newValue === '') {
        return;
    }
    config.error_flag = false;
    g_form.hideFieldMsg(config.field, true);
    var regexGoodForm = new RegExp("^\\([\\d]{"
      + config.sections.one
      + "}\\)\\s[\\d]{"
      + config.sections.two
      + "}\\-[\\d]{"
      + config.sections.three
      + "}$", "g"
    );
    var isGoodForm = regexGoodForm.test(newValue);
    //if value does not match (n1) n2-n3 format
    if (!isGoodForm) {
        var regexNonDigits = /[^\d]/g;
        // remove all non number from string
        var userPhone = newValue.replace(regexNonDigits, '');
        // verify if number is x digits
        var regexXDigits = new RegExp("[^d]\{"
                            + config.required_length
                            + "\}", "g");
        var isXDigits = regexXDigits.test(userPhone);
        if (isXDigits) {
            // if true, reformat to (n1) n2-n3
            var onePlusTwo = config.sections.two + config.sections.one;
            var onePlusTwoPlusThree = config.sections.three + onePlusTwo;
            var newNum = '';
            newNum += '(' + userPhone.substring(0, config.sections.one) + ') ';
            newNum += userPhone.substring(config.sections.one, onePlusTwo) + '-';
            newNum += userPhone.substring(onePlusTwo, onePlusTwoPlusThree);
            g_form.setValue(config.field, newNum);
        } else {
            // if true not, trigger error
            config.error_flag = true;
        }
        if (config.error_flag) {
            g_form.clearValue(config.field);
            g_form.showFieldMsg(config.field, config.error, 'error');
        }
    }
}`
---
[View this page on GitHub](https://github.com/jacebenson/jace.pro/tree/main/./src/posts/2017/2017-09-25-validating-phone.md).

[Client Script - Validating Phone](https://jace.pro/blog/client-script-validating-phone/) [Jace Benson](https://jace.pro) ![Jace Benson](https://jace.pro/icon-512x512.png)

---

*This content is from Jace Benson's ServiceNow and tech blog at jace.pro*
*Original post: https://jace.pro/blog/client-script-validating-phone/*
