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

Chris M3
Tera Guru

So, you have a couple options.   In your validation, when you display the alert you could clear the value out in the field, thus they need to enter from scratch again.   Then the required check will work correctly.   Secondly, you can have an OnSubmit script that validates it again and rejects the submission if it's not correct.



For something like this one, I would probably clear the value.



Also, for min length, when you're checking if it's a number you can also do a .length == 7 check and just update the message to reflect both requirements.


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

This works perfectly!



thank you both very much for the feedback!


Hi Katelyn,



Can you mark the answer as correct and hit like. Thanks in advance.



Regards


Ankur


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