- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 07:23 AM
Hello Devs,
We have a string type variable in catalog item. And we want that variable to accept only integers greater than 8. I am using below regex in my script and it does not accept any number greater than 9. But I want that variable to accept any number greater than 8 but no less than 8.
regex: /^[8-9]*$/
How can I achieve that?
Thanks,
Rocky.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 07:47 AM
this regex worked for me
^(?:[9]|\d\d\d*)$
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 01:27 PM - edited 05-23-2024 12:16 AM
Hi @Rocky5 ,
You can try below onchange client script,
first it checks the regex and whether the input is a number or not then it check the input is greater than 8 ,
Change the backend name according to your catalog item
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var number = g_form.getValue('inte'); // Get the value of the integer field
var regex = /^[0-9]+$/;// Define the regex to check if the input is a number
// Check if the input matches the regex
if (!regex.test(number)) {
alert('Please enter a valid number.');
return false;
}
// Convert the input to a number and check if it is greater than 8
if (parseInt(number, 10) <= 8 ) {
alert('The number must be greater than 8.');
g_form.clearValue('inte');
return false;
}
return true;
}
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 02:48 PM
@Rocky5 ,
Please try regex=/^(?:[9-9]\d*|[1-9]\d{1,})$/
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar