Catalog item - Validation for phone number - variable - On change client script

PHOTO EMBED

Thu Jun 20 2024 05:40:57 GMT+0000 (Coordinated Universal Time)

Saved by @piyranja3 ##regex ##validation ##phone_number

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var pattern = /^[(]?(\d{3})[)]?[-|\s]?(\d{3})[-|\s]?(\d{4})$/;

    if (!pattern.test(newValue)) {
        alert('Phone enter a valid phone number');
        g_form.setValue('variable_name', '');
    }
}
content_copyCOPY

--Resolution 1. Create a variable of Single Line Text type. 2. Create the following onChange client script: 3. Please note that the above regex / pattern is specific for validating 10 integer digits only. You may modify this regex if there's a need for it to validate any other formats.