I have created a variable type as single line text but i want that it should only accept numeric values and it should not allow number greater than 10

snow34
Tera Contributor

I have created a variable type as single line text but i want that it should only accept numeric values and it should not allow number greater than 10  , how to achieve this with the help of client script 

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

Hi,

Did you try using Validation Regex? you can do this without scripting.

> From your left navigator, open 'Variable validation regex'

> Create a new record with regular expression as \b([0-9]|10)\b

> Give required message.

> Save.

Now open your variable in catalog item

> In Type Specification tab, in validation regex field, select the regex which is created in above steps.

>Done.

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

 

 

 

View solution in original post

9 REPLIES 9

ServiceNow De14
Tera Contributor

Hi

Validate with a RegEX using OnChange client script and display an info message.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so what did you start with and where are you stuck?

regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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

var num=newValue;

if(isNaN(num)){

g_form.addInfoMessage('Numbers is acceptable as hours');

g_form.clearValue('<weekly_total_working_hours>');

return;

}

else if(parseInt(num) >=1 || parseInt(num) <= 45);

alert('Numbers from 1 to 45 are allowed');

//g_form.clearValue('<weekly_total_working_hours>');
    g_form.setValue('weekly_total_working_hours','');

}

 

 

I have written this script which is not working as expected .

Hi,

Did you try the regex i mentioned?

Or else you can update your script as below and try

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

if(isNaN(parseInt(newValue)){
g_form.addInfoMessage('Numbers is acceptable as hours');
g_form.clearValue('weekly_total_working_hours');
}

else if((parseInt(newValue) <1) || (parseInt(newValue) > 45))
{
alert('Numbers from 1 to 45 are allowed');
g_form.clearValue('<weekly_total_working_hours>');
}
}

 

Regards,
Sumanth