Prevent catalog submission based on date field in text box (string field) in MRV

reddy8055
Tera Contributor

Hi,

I am populating date field in string field in MRV and want to prevent submitting the form if any of the date is more than 365 days with today's date and if it falls less than 365 days then form should be submitted.  I have written onsubmit client script but Its not checking for all date fields (in rows)

reddy8055_0-1668743136588.png

 

 

function onSubmit() {
var today = new Date();
//today.setHours(0,0,0,0);
var startDTe = g_form.getValue('course_completed_date');
//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) {
return false;
}

return true;
}

 

Thanks,

10 REPLIES 10

Brad Bowman
Kilo Patron
Kilo Patron

Assuming your date methods and logic works in a client script, here's how to evaluate each row of the MRVS, and prevent submission if any are outside the date parameter:

function onSubmit() {
    var today = new Date();
    var mrvs = g_form.getValue('mrvs_internal_name'); // internal name of your 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) {
            return false;
        }
    }
}

 

Hi Brad,

I tried this and its not working

Help me help you.  Are you getting the coursedate alert, but it's not the expected value, or are you not getting that alert at all?  If you add an alert on the mrvs script variable after the var mrvs, line what is the result?  Uncomment your other alerts, and add some more to see what the script is doing/evaluating, and you should see where there is a logic or syntax error.

Iam getting the below error.

 

ERROR:

onSubmit script error: SyntaxError: Unexpected end of JSON input:
function () { [native code] }

 

Client script:

function onSubmit() {
    var today = new Date();
    var mrvs = g_form.getValue('date'); // internal name of your MRVS
    var obj = JSON.parse(mrvs);
    for (var i = 0; i < obj.length; i++) {
        var startDTe = obj[i].vta; // 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) {
            return false;
        }
       
    }
 return true;
}