Required minimum characters in service catalog variable

katelynlassan
Kilo Contributor

I have an item in my Service Catalog that needs to have a single line text field that will only allow a numeric value and no less than 7 digits.

i am using the following client script which prompts a pop up box warning stating a number is needed for this field, but once you acknowledge the box, the system considers the field fulfilled even though there are still letters in the field as opposed to numbers. It will still let me submit the request with letters in that field.

find_real_file.png

Also, i am using the following variable attribute to allow only 7 digits in the field, is there a way to require at least seven as opposed to no more that 7?

find_real_file.png

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Katelyn,



If I have read your comment correctly then following is your requirement:


1) The field should have only digits


2) Also the length should be exactly 7 and not less than that and not more than that



If yes then following is the approach:


1) Have a onChange script on this variable and check whether it's a digit. you can use isNaN() method of javascript and if not clear the field


2) Second you have to check whether the length is exactly 7 so you can use length property on that variable



So here is your code


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


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


          return;


    }



// first check whether it is a digit


var fieldValue = value;


if(!isNaN(fieldValue)){


alert("This field should have only digits");


g_form.clearValue('field_name');


}



// now check the length


if(fieldValue.length != 7){


alert("Field should have exact 7 digits");


g_form.clearValue('field_name');


}



}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

View solution in original post

11 REPLIES 11

i was able to like the post, but i do not see an action to mark it as the correct answer on my page. I have seen this before, but for some reason do not see it now.


Your post was not originally marked as a question, only questions have answers


Ah. Thank you, Chris!


I believe dan.bruhn can switch it to a question for you.


I pretty much have the same scenario.   We have a Single Line Text variable on our form that MUST be exactly 6 numbers.   So I tried incorporating this variable script, but cannot get it to work.   I get a JavaScript error, and then it accepts any entry I make.



Here is my script:


find_real_file.png


The only difference it is running against a Variable Set, but that shouldn't matter, should it?



Here is the error message I get:


"There is a JavaScript error in your browser console", and then it accepts the entry, even though it doesn't meet the criteria.



Any idea why this isn't working?