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

Regular expression to restrict 20 characters in single line text variable

Rajesh Mushke
Mega Sage

HI All,

I need help to build regular expression to restrict enter 20 numbers & below characters in single line text.

20 Numeric and only the special characters that include: + ( ) - 

 

Thanks in advance.!

 

Thanks,

Rajashekhar Mushke



Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke
17 REPLIES 17

reginabautista
Kilo Sage

Hi try this:

 

^[\d ()-]{1,20}$

 

Thanks

Regina

SatheeshKumar
Kilo Sage

Hi Rajashekar,

The below script will do the required checks.

var test_string = "1234";// replace with your form variable

var regex = /^[\+\-()0-9]{0,10}$/;

var result = regex.test(test_string);

alert(result);

 

Also make use of madrid  Variable Validation Regex.

For more details on using Variable Validation Regex check the below link.

https://community.servicenow.com/community?id=community_article&sys_id=e334bc4cdbe0bf48fff8a345ca961...

 

-Satheesh

 

 

prathyusha14
Tera Contributor

Hi,

Please try this code for above Requirement.It's Working fine on my DEV.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var phone = g_form.getValue('phone_number');// Phone_number is my variable name
var answer = true;
var param =/^[0-9-+()]{1,15}$/;
if (!phone.match(param))

{

g_form.addErrorMessage('Only valid Phone numbers are allowed');
g_form.clearValue('phone_number');
answer = false;
}
else if (phone == /^[0-9-+()]{1,15}$/)
{
g_form.setValue('phone_number');

} else {
answer = true;
}
return answer;
}

 

Thanks,

Prathyusha.