Question

I have a small request. I am validating few fields in pre save function through j query. The problem that i am stuck on is that, i have a drop down named = "Is the Process flow document available?" and "Frequency of deliverable" which are visible only when the single line text field "Nature of work" contains "Execute Business processes or Deliver periodic reports". Now i am able to check there validation when the "Nature of field contains" the above two mentioned values but when the text field does not contain any of the value these two fields are hidden in the form , but when the validation runs it checks for the hidden fields too. I want to perform validation only when the fields are visible in the form.

Here is my code for hiding the fields when single line text field not equal to the values specified above

var txt = $("input[title='Nature of the work']").val();
 if ((txt != "Execute Business processes") && (txt != "Deliver periodic reports"))
 {
    $('nobr:contains("Is the Process flow document available")').closest('tr').hide();
$('nobr:contains("Frequency of the Deliverables")').closest('tr').hide();
}
else
{
  $('nobr:contains("Is the Process flow document available")').closest('tr').show();
  $('nobr:contains("Frequency of the Deliverables")').closest('tr').show();
}

Here is my code for validtaion

fucntion PreSaveAction(){
 point of contact = $("select[title='Is the Process flow document available']").text();
 Frequencydeliverables = $("select[title='Frequency of the Deliverables']").text();
 if(point of contact=="Select a Value")
 {
 alert("Please select an appropriate value");
 $("select[title='Is the Process flow document available']").focus();
 return false;
 }
 if(Frequencydeliverables=="Select a Value")
  {
  alert("Please select an appropriate value");
   $("select[title='Frequency of the Deliverables']").focus();
  return false;
  }
return true;
}

If you guys can help me out please do thank you !

Was it helpful?

Solution

You can also use the same condition in your PreSaveAction function so the validation will only runs when the fields are visible

fucntion PreSaveAction(){
     var txt = $("input[title='Nature of the work']").val();
     if ((txt == "Execute Business processes") || (txt == "Deliver periodic reports"))
     {
          point of contact = $("select[title='Is the Process flow document available']").text();
          Frequencydeliverables = $("select[title='Frequency of the Deliverables']").text();
          if(point of contact=="Select a Value")
          {
              alert("Please select an appropriate value");
              $("select[title='Is the Process flow document available']").focus();
                 return false;
          }
          if(Frequencydeliverables=="Select a Value")
          {
              alert("Please select an appropriate value");
               $("select[title='Frequency of the Deliverables']").focus();
                    return false;
          }
                return true;
       }
       else
          return true;
  }

OTHER TIPS

Try looking for the display attribute and base you logic off of that. When you hide the element, it gives it a style="display:none;" attribute. Can you get it that way? or even better, change the class of the element your are changing and find it that way.

if ((txt != "Execute Business processes") && (txt != "Deliver periodic reports"))
{
    $('nobr:contains("Is the Process flow document available")').closest('tr').hide();
    $('nobr:contains("Frequency of the Deliverables")').closest('tr').hide();
}
else
{
$('nobr:contains("Is the Process flow document available")').closest('tr').show();
$('nobr:contains("Frequency of the Deliverables")').closest('tr').show();
    $('nobr:contains("Frequency of the Deliverables")').closest('tr').attr("class", "showing");
    $('nobr:contains("Is the Process flow document available")').closest('tr').attr("class", "showing");
}



fucntion PreSaveAction(){
   point of contact = $("select[title='Is the Process flow document available']").text();
   Frequencydeliverables = $("select[title='Frequency of the Deliverables']").text();                                
                                                  //take the spaces out of your vars
       if((point of contact=="Select a Value") && (!pointofcontact.hasClass("showing"))
       {
           alert("Please select an appropriate value");
           $("select[title='Is the Process flow document available']").focus();
       return false;
       }
       if((Frequencydeliverables=="Select a Value" && (!Frequencydeliverables.hasClass("showing")))
       {
            alert("Please select an appropriate value");
            $("select[title='Frequency of the Deliverables']").focus();
       return false;
       }
       return true;
}

Just an idea. This might work for you. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top