- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2015 12:54 PM
I've got a field that can only have 6 digits as valid input. I've got an 'On-Change' client script that checks the input, and is supposed to display a field message and clear the input if it's not all digits. The field length is set to '6' to handle the length. Here's the script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
//reg checks for just digits in u_similar_prod and displays an error if there are letters
var ans = newValue;
var reg = /^[0-9]+$/;
if(!reg.test(ans)) {
alert('Only numbers' + ans);
g_form.showFieldMsg('u_similar_prod','This field can only contain a 6-digit product code','error');
g_form.setValue('u_similar_prod','');
}
}
The alert is in there just to be sure the 'if' is true. The alert fires, and the value is cleared, but the show field message doesn't display. I can change the script to display the message on a different field, and it displays just fine. I've tried moving the field 'u_similar_prod' to a different spot on the form to no avail. I've set the field length to 40, thinking maybe having it be so short wasn't allowing enough room for the message or something. No joy. I'm stumped.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2015 02:03 PM
I'm not entirely sure why this worked, but I moved the 'setValue' empty before the 'showFieldMsg' and now I'm getting the message. I'm guessing that clearing the value was registered as a change, so it re-ran the script, and the test was false so the message didn't display.
This is different than the way this behaves in a catalog client script, where the message, once displayed, is there until you hide it. Weird.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2015 02:03 PM
I'm not entirely sure why this worked, but I moved the 'setValue' empty before the 'showFieldMsg' and now I'm getting the message. I'm guessing that clearing the value was registered as a change, so it re-ran the script, and the test was false so the message didn't display.
This is different than the way this behaves in a catalog client script, where the message, once displayed, is there until you hide it. Weird.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-13-2017 03:52 AM
g_form.setValue should clear info and error field messages on the form, but has not been consistently doing that in all situations, such as for reference fields, Catalog variables, and in the Service Portal. These known problems have been fixed for Jakarta I believe.
If you want to use setValue and showFieldMsg in the same client script on the same field/variable, then do the setValue first. You can also avoid needing to run hideFieldMessage first if you do that.
Cheers,
Dave
p.s.Also always remember to only run setValue if you actually are changing the value, as it will also trigger this message clearing behaviour if the new value is the same as the existing one. A simple IF comparing the current value and the one you are about to set can avoid running it.
p.p.s The additional benefit of that is you also avoid causing infinite recursion in case your client script itself is triggered onChange of that same variable.