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

I tested this and received the same in Scripts - Background. There must be something else at play that is causing this to not remove the straight text. I simply cannot find it.


ctomasi did you test using plain text as well without any numbers or just the string with text and numbers? As I said, it will strip the field if it has numbers as well as text. If plain text, will remove this data


venkatiyer1
Giga Guru

Hi Kevin,



You may want to replace if (newValue) with if(newValue.trim() != "") or if(newValue.trim())



The reason being if you enter without numbers it will remove the text and make it an empty string if it has empty space in it since we are not trimming it will pass through as true.


I appreciate this information and will try this.


Vladi1
Kilo Guru

This is my script for accepting numbers only


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


  if (isLoading || newValue == '') {


  return;


  }


  var regexp = /^[+]?\d*$/;


  if(!regexp.test(newValue)){


  alert('Please enter numeric value');


  g_form.setValue('v_sec_review_unknown','');


  }


}



in addition I defined "variable attributes   max_length=3" to accept up to 3 digits



This is one of my sources:


Regular Expression Library