Prevent Submitting the form if Multi row variable set is empty

reddy8055
Tera Contributor

Hi,

I am populating multi row variable set based on requested for user info and want to prevent submitting the form whenever MRVS is empty. Tried mrvrs == '' like highlighted below in red but its not working.

 

function onSubmit() {
var today = new Date();
var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
alert(mrvs);
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
var startDTe = obj[i].course_completed_date; // name of the MRVS variable
//alert(startDTe);
var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
var coursedate = today - startDateNum;
//alert(coursedate);
var dayDifference = coursedate / (1000 * 60 * 60 * 24);
alert(dayDifference);
if ((dayDifference > 365) || (mrvs == '') || (mrvs == null)) {
g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
return false;
}
}

 

Thanks,

11 REPLIES 11

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

You an just use individual variable name in the mvrs to validate this. If you want to stop prevention if any variable is empty then is it as field1==''|| field2==''

 

and if you want want prevent only if all the mvrs field are empty use field1==''&&field2==''

 

https://www.servicenow.com/community/it-service-management-forum/validation-on-multi-row-variable-se...

 

Please mark my answer as correct based on Impact.

Hi,

 

Tried like the below its not working. I am populating mrvs like tabular format so want to stop if all variables(whole set ) is empty

 

if ((dayDifference > 365) && (mrvs == '') && (mrvs == null)) {

 

Thanks,

Hello,

 

Yes that is waht i was saying instad of taking the complete mvrs use the variables individually like below:-

 

if ((dayDifference > 365) && (mvrsfiedl1=='') && (mvrsfiedl2=='') && (mvrsfiedl2=='')) {

 

Please mark my answer as correct based on Impact.

 

Its not working.

 

function onSubmit() {
var today = new Date();
var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
var x = g_form.getValue('course_completed_date');
var y = g_form.getValue('name');
var z = g_form.getValue('email');
var total = 0;
alert(mrvs);
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
var startDTe = obj[i].course_completed_date; // name of the MRVS variable
total = total + parseInt(parser[i]).course_completed_date; // give here the variable name for proportion
alert(total);
//alert(startDTe);
var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
var coursedate = today - startDateNum;
//alert(coursedate);
var dayDifference = coursedate / (1000 * 60 * 60 * 24);
alert(dayDifference);
if ((dayDifference > 365) && (mrvs == '') && (x == '') && (y == '') && (z == '')) {
g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
return false;
}
}

Thanks,