The CreatorCon Call for Content is officially open! Get started here.

Regex to strip all characters except digits

kevin_eldridge
Kilo Guru

I have made an attempt to use a Regex in Geneva Patch 7 that strips a passed string of all characters except digits using something like the following on onChange Catalog Client Script for the phone variable:

function onChange(control, oldValue, newValue, isLoading) {

        // If the newValue is empty, return

        if (isLoading) {

                  return;

        }

        // If the newValue is a completely new value and does not equal the previous value, perform the following validations

        var tempBusPhone = newValue;

        tempBusPhone = tempBusPhone.replace(/[^\n\d]+/g, '');

        // tempBusPhone = tempBusPhone.replace(/[^0-9]/g, '');

        if (newValue) {

                  if (tempBusPhone.length == 4) {

                            // If the length of the value is 4 characters, place "ext " in front of the number

                            tempBusPhone = tempBusPhone.replace(/(\d{4})/, 'ext $1');

                            g_form.setValue('phone', tempBusPhone);

                  } else if (tempBusPhone.length == 10) {

                            // If the length of the value is 10 characters, format the number as (###) ###-####

                            tempBusPhone = tempBusPhone.replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');

                            g_form.setValue('phone', tempBusPhone);

                  } else {

                            g_form.showFieldMsg('phone',"This user's phone number must be in the format of 4-digits or 10-digits",'info');

                  }

        }

}

Edit: I forgot to mention that phone is being pulled from the User [sys_user] table for the current logged in user. Most people do not have phone numbers entered for their profile.

However, I can still enter in plain text and it will be accepted. If I should enter plain text and numbers, it will remove the text. Can anyone help me with stripping the plain text? Nothing I have tried will strip the straight text. I looked into ServiceNow's regex syntax, but have not figured that out yet.

SNC Regex API - ServiceNow Wiki

Using Regular Expressions in Scripts

Thank you for your assistance,

Kevin Eldridge

Message was edited by: Kevin Eldridge

9 REPLIES 9

Chuck Tomasi
Tera Patron

Hey Kevin,



FYI - I think your RegEx is fine. I just did this test in Scripts Background and it worked fine.



var tempBusPhone = 'string1string2string3';  


tempBusPhone = tempBusPhone.replace(/[^\n\d]+/g, '');


gs.print(tempBusPhone);



Output: 123


You may want to put in a few debug statements to check the variables along the way. Ensure your statements are working in the order and as expected.


ctomasi



I did this and it is firing as it should. However, when plain text is entered into the field, it is kept and it does not matter how long the text string is, it will be left as a valid phone number.



If a number or other non-letter number is entered in with it, it will be stripped.



The trim() that was mentioned does trim the value just in case someone enters in spaces and displays a message.


When I use a test string of



'stringOtherString', the result is blank.