How to validate multiple fields OnSubmit & show alert messages using single servienow functionality?

Novin Ramteke
Tera Contributor

I have a scenario where if the data in Task time fields is greater than zero, and the user tries to submit the form, he should get an error message to fill in the associated Task details.

For example, when user fills Task 3 time and Task 4 time values greater than zero, and tries to submit the form, he should get an error message that it's associated Task 3 details and Task 4 details should be filled.

I am trying to achieve it with a single functionality and avoid writing multiple onSubmit client scripts. Please help me with a script logic or an approach to implement it.scenario.png

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Novin Ramteke ,

 

You can use below script to achieve the same:

function onSubmit() {
      var task1 = parseInt(g_form.getValue('task_1'));
      var task2 = parseInt(g_form.getValue('task_2'));
      var task3 = parseInt(g_form.getValue('task_3'));
      var task4 = parseInt(g_form.getValue('task_4'));
      var task5 = parseInt(g_form.getValue('task_5'));
     var fields = [];
      if(task1>0){
              if(g_form.getValue("task_details_1") == ''){
                       fields.push("task_details_1");
      }
-     if(task2>0){
              if(g_form.getValue("task_details_2") == ''){
                       fields.push("task_details_2");
      }
      if(task3>0){
              if(g_form.getValue("task_details_3") == ''){
                       fields.push("task_details_3");
      }
      if(task4>0){
              if(g_form.getValue("task_details_4") == ''){
                       fields.push("task_details_4");
      }
      if(task5>0){
              if(g_form.getValue("task_details_5") == ''){
                       fields.push("task_details_5");
      }
  if(fields.length != 0){
		g_form.addErrorMessage("Fill task details for these fields, since time is greater than 0 "+ fields);
		return false;
	}
	
}
Best Regards
Aman Kumar

View solution in original post

5 REPLIES 5

Mohan raj
Mega Sage

Hi @Novin Ramteke,

 

I try this code it was working for me

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var task1 = g_form.getValue('task_1');
   var task2 = g_form.getValue('task_2');
   var task3 = g_form.getValue('task_3');
   var task4 = g_form.getValue('task_4');
   var task5 = g_form.getValue('task_5');
	var sum = task1+task2+task3+task4+task5;
	if(sum > 0){
		g_form.addErrorMessage("Task time fields is greater than zero");
		return false;
	}
	
}

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Regards,

Mohan.

Thanks @Mohan raj, but on a real form, I have more than 12 Task time fields and I have to inform the user through an error message about the exact Task details fields he needs to fill (he has not filled). Thanks again, I appreciate your help.

Aman Kumar S
Kilo Patron

Hi @Novin Ramteke ,

 

You can use below script to achieve the same:

function onSubmit() {
      var task1 = parseInt(g_form.getValue('task_1'));
      var task2 = parseInt(g_form.getValue('task_2'));
      var task3 = parseInt(g_form.getValue('task_3'));
      var task4 = parseInt(g_form.getValue('task_4'));
      var task5 = parseInt(g_form.getValue('task_5'));
     var fields = [];
      if(task1>0){
              if(g_form.getValue("task_details_1") == ''){
                       fields.push("task_details_1");
      }
-     if(task2>0){
              if(g_form.getValue("task_details_2") == ''){
                       fields.push("task_details_2");
      }
      if(task3>0){
              if(g_form.getValue("task_details_3") == ''){
                       fields.push("task_details_3");
      }
      if(task4>0){
              if(g_form.getValue("task_details_4") == ''){
                       fields.push("task_details_4");
      }
      if(task5>0){
              if(g_form.getValue("task_details_5") == ''){
                       fields.push("task_details_5");
      }
  if(fields.length != 0){
		g_form.addErrorMessage("Fill task details for these fields, since time is greater than 0 "+ fields);
		return false;
	}
	
}
Best Regards
Aman Kumar

Thanks, @Aman Kumar S, the above logic worked perfectly for me. Except, a few curly brackets were missing, the logic is exactly what I was looking for. Thanks again, I appreciate your help. Adding the working script below.

 

function onSubmit() {
    var task1 = parseInt(g_form.getValue('u_task_1_time'));
    var task2 = parseInt(g_form.getValue('u_task_2_time'));
    var task3 = parseInt(g_form.getValue('u_task_3_time'));
    var task4 = parseInt(g_form.getValue('u_task_4_time'));
    var task5 = parseInt(g_form.getValue('u_task_5_time'));
    var fields = [];
    if (task1 > 0) {
        if (g_form.getValue("u_task_1_details") == '') {
            fields.push(" Task 1 details");
        }
    }
    if (task2 > 0) {
        if (g_form.getValue("u_task_2_details") == '') {
            fields.push(" Task 2 details ");
        }
    }
    if (task3 > 0) {
        if (g_form.getValue("u_task_3_details") == '') {
            fields.push(" Task 3 details ");
        }
    }
    if (task4 > 0) {
        if (g_form.getValue("u_task_4_details") == '') {
            fields.push(" Task 4 details ");
        }
    }
    if (task5 > 0) {
        if (g_form.getValue("u_task_5_details") == '') {
            fields.push(" Task 5 details ");
        }
    }
    if (fields.length != 0) {
        g_form.addErrorMessage("Fill task details for these fields"  + fields  +" since time is greater than 0 ");
        return false;
    }
}