- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 02:23 PM
Hi,
New to SeviceNow and trying to figure Catalog Client Scripts to validate and show an error on a form it is not working as expected. If I use two sets of criteria such as length of the newValue in the field and a specific country from a select box it works ok, but if I add in additional criteria for newValue field such where it does not start with "OC" it does not work properly. My script is:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == ''){
return;
}
var length=newValue.length;
var eng=newValue.substring(0, 2);
if ((length =6) && (g_form.getValue('u_country_uk') == 'United Kingdom') && (eng != "OC"));
{
alert("invalid characters");
return false;
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 03:18 PM
Few issues:
1. unless you are testing for a case sensitive "OC", you should force to lower case (or upper, doesn't really matter which one) before doing the compare, otherwise "oc" is not equal to "OC"
2. "length =6" should be "length == 6" to check to see if it is 6 characters long
3. no need to "return false" in an onChange
4. there was a ";" at the end of the if condition that may have broken things as well
Try this out:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == ''){
return;
}
var length = newValue.length;
var eng = newValue.substring(0, 2).toLowerCase(); //force to lowercase for compare later on
if (length == 6 && g_form.getValue('u_country_uk') == 'United Kingdom' && eng != "oc")){
alert("invalid characters");
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 02:28 PM
Hi Joe,
Try adding a temporary alert on eng right after the var eng line to confirm the value stored in that variable. It looks like you are returning 3 characters, but only using 2 in your if test. Since these are all && you don't need the extra parenthesis around the first 2 criteria, but it doesn't hurt anything. An alternate method to substringing the first 2 characters in this case would be && newValue.indexOf('OC') !=0 meaning it doesn't start with 'OC'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 02:44 PM
Hi Brad,
I added the alert after the var eng, but it only returns 2 characters. I do not understand your remark on returning 3 characters, could you please explain?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 04:26 PM
Just wanted to confirm eng contained what you were expecting. Jim's response covers the other 2-3 errors, and one unnecessary line in your script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 03:18 PM
Few issues:
1. unless you are testing for a case sensitive "OC", you should force to lower case (or upper, doesn't really matter which one) before doing the compare, otherwise "oc" is not equal to "OC"
2. "length =6" should be "length == 6" to check to see if it is 6 characters long
3. no need to "return false" in an onChange
4. there was a ";" at the end of the if condition that may have broken things as well
Try this out:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == ''){
return;
}
var length = newValue.length;
var eng = newValue.substring(0, 2).toLowerCase(); //force to lowercase for compare later on
if (length == 6 && g_form.getValue('u_country_uk') == 'United Kingdom' && eng != "oc")){
alert("invalid characters");
}
}