0

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 !

1

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;
  }
  • this looks promising lets see if this works out. – jayant khushalani Apr 14 '18 at 6:09
  • hey Saeed i would also like to know how to append an error message to the field rather than the user clicking okay on the alert message is it possible to do that ? – jayant khushalani Apr 14 '18 at 6:21
  • hi saeed i tried the above method and it seems there is a tiny mistake in this. Updated answer : point of contact = $("select[title='Is the Process flow document available'] option:selected").text(); – jayant khushalani Apr 16 '18 at 4:51
  • hi @jayantkhushalani i only added the condition and it didn't revise your function i took it copy & paste assuming you're already tested it – A.Saéed Apr 16 '18 at 9:15
  • yes i tested the above function and it is working with a tweak that i gave. i also had a doubt regarding what i spoke in my earlier comments in another question. i want to show the error message dynamically as soon as the user enters the data in text box i want to the error to not be visible. – jayant khushalani Apr 16 '18 at 9:24
0

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.

  • this is not working for me but currently in my code there is an error Frequencydeliverables = $("select[title='Frequency of the Deliverables'] option:selected").text(); but then again i did the exact thing you told me and it is not working. – jayant khushalani Apr 13 '18 at 12:00
  • if you just copied and pasted it, there is an error with how you declared your variables. I would try to clean that up a bit. you can use title*= to get part of the title instead of the whole thing. that should help a bit. Also, look in the DEV toolbar to mak esure the class is getting set. – lazoDev Apr 13 '18 at 12:43
  • sure i would try this and get back to you. – jayant khushalani Apr 14 '18 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.