- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2019 07:38 PM
Hi all,
I have created an onChange catalog client script on a variable set on specific variable, and used:
g_form.showFieldMsg('variable_name', 'Test comment showed', 'error');
The msg shows under the field, however, it still allows the form to be submitted. Looking at the below KB this should not allow form to be submitted, any ideas if this is changed recently?
https://hi.service-now.com/kb_view.do?sysparm_article=KB0720733
Solved! Go to Solution.
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2019 12:03 AM
You will require to write on submit script as well. use below script for onchange
if(newValue == '0' || newValue == '1') {
g_form.showFieldMsg('VARIABLE_NAME', 'Cost cannot start with 0 or 1', "error");
}
var validate = new RegExp('^[0-9-_/:]+$');
var result = validate.test(newValue);
if(!result) {
g_form.showFieldMsg('VARIABLE_NAME', 'Cost cannot have spaces or special characters other than - or /.', "error");
}
if(newValue.length < 6 && newValue.length !=0) {
g_form.showFieldMsg('VARIABLE_NAME', 'Cost cannot be less than 6 digits', "error");
}
}
Use below code for the onSubmit
var newValue = 'Get value of variable';
if(newValue == '0' || newValue == '1') {
g_form.showFieldMsg('VARIABLE_NAME', 'Cost cannot start with 0 or 1', "error");
}
var validate = new RegExp('^[0-9-_/:]+$');
var result = validate.test(newValue);
var flag = true;
if(!result) {
g_form.showFieldMsg('VARIABLE_NAME', 'Cost cannot have spaces or special characters other than - or /.', "error");
flag = false ;
}
if(newValue.length < 6 && newValue.length !=0) {
g_form.showFieldMsg('VARIABLE_NAME', 'Cost cannot be less than 6 digits', "error");
flag = false;
}
if(flag == false)
return false ;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2019 08:16 PM
Hi sam
g_form.showFieldMsg('variable_name', 'Test comment showed', 'error'); // This function only displays the error message. This will not restrict the user to submit the form. If you want to restrict then set the field mandatory in your client script.
I saw the KB link that you have provided. There was a bug which was restricting the user to submit the form when using this function which is resolveLondonondon patch 6 and madrid release
-Harsh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2019 08:21 PM
The field is mandatory through UI policy, so you suggest i add below to my code?
g_form.showFieldMsg('variable_name', 'Test comment showed', 'error');
g_form.setMandatory('variable_name', true);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2019 08:39 PM
I will suggest, keep the mandatory in UI policy only. You can clear the field after showing the error message.
g_form.showFieldMsg('variable_name', 'Test comment showed', 'error');
g_form.clearVlaue('variable_name');
So the field will become empty and you already have a UI policy to check mandatory. So the user will not be able to submit hte form
-Harsh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2019 10:51 PM
Problem doing that is, if they mis typed and we clear the variable then they will have to start typing whole thing again, so I would prefer not to clear the field - just not allow them to submit the form till they have corrected the error.
My code is below for onChange. I read on another thread to make exact same script for onSubmit - but when doing that it does not work as i believe i will possibly need to change below code which i am not sure off.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
if(newValue[0] == '0' || newValue[0] == '1') {
g_form.showFieldMsg('variable_name', 'Cost cannot start with 0 or 1', "error");
}
var validate = new RegExp('^[0-9-_/:]+$');
var result = validate.test(newValue);
if(!result) {
g_form.showFieldMsg('variable_name' "Cost cannot have spaces or special characters other than - or /.', "error");
}
if(newValue.length < 6 && newValue.length !=0) {
g_form.showFieldMsg('variable_name', 'Cost cannot be less than 6 digits', "error");
}
}
I have also seen another thread to add this to not allow to submit, but not sure how to implement this into my above script. https://community.servicenow.com/community?id=community_question&sys_id=5e9bcb61db9cdbc01dcaf3231f96...
ok_to_submit = ok_to_submit = 0 + 1;
}
} // if (isNaN(IntgerString))
// ALERT/Submit code
if ( ok_to_submit == 0) {
return true;
} else {
alert('Your submission has errors. Please check the form for errors messages.');
return false;
}
}