none
Sharepoint form validation using Javascript RRS feed

  • Question

  • I have the following JavaScript that I modified. However, I can not seem to make it work. Could someone assist?

    <script type="text/javascript" src="/jquery-1.12.4.min.js"></script>
     <script type="text/javascript" language="javascript">
     function PreSaveAction() {
    drop = $("select[title='Stage']").val();
    date = $("input[title='Date This Action Is Released in PD2']").val();
        if (drop ==="Complete" && date===""){
            alert("The Date This Action Is Released in PD2 is required due to the selection of the Stage equaling Complete. Please enter a date for Date This Action Is Released in PD2");
            return false;
        }   
            return true;
    }
     </script>


    Lee Mossolle

    Thursday, June 13, 2019 3:31 PM

Answers

  • Hi,

    Try providing the jquery-1.12.4.min.js in style library and provide the Url from there just like below.

    <script src="
    https://global.sharepoint.com/:u:/r/sites/treys/Style%20Library/jquery-1.12.4.min.js"
           type="text/javascript"></script>

    <script type="text/javascript">
     function PreSaveAction() {
    drop = $("select[title='Stage']").val();
    date = $("input[title='Date This Action Is Released in PD2']").val();
        if (drop ==="Complete" && date===""){
            alert("The Date This Action Is Released in PD2 is required due to the selection of the Stage equaling Complete. Please enter a date for Date This Action Is Released in PD2");
            return false;
        }   
            return true;
    }
     </script>

    Thanks & Regards,


    sharath aluri

    • Marked as answer by lmossolle Monday, November 25, 2019 6:03 PM
    Thursday, June 13, 2019 5:18 PM
  • Hi Im,

    Below article is the same kind of requirement they have other than just changing fields according to your requirement.

    https://social.msdn.microsoft.com/Forums/office/en-US/481f4f90-5d65-44de-a408-5e1dc3017ffb/how-to-make-another-column-in-a-list-as-mandatory-based-on-choice-column-value-in-sp-2010-custom?forum=sharepointcustomizationprevious

    Thanks & Regards,


    sharath aluri


    Thursday, June 13, 2019 11:35 PM
  • The other and easiest way of doing without Jquery is: Go to List -> List Settings -> Validation Settings apply the formula that i provided in the down. I changed the formula as per your requirement:

    Formula:

    =IF(Stage="Complete",IF([Date This Action Is Released in PD2]<>"",TRUE,FALSE),TRUE)=IF(Stage="Canceled",IF([Date This Action Is Released in PD2]<>"",TRUE,FALSE),TRUE)

    Thanks & Regards,


    sharath aluri

    • Marked as answer by lmossolle Monday, November 25, 2019 6:03 PM
    Friday, June 14, 2019 12:33 AM
  • Hi Imossolle,

    Modified the code snippet as below:

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
     function PreSaveAction() {
    drop = $("select[title='Stage']").val();
    date = $("input[title='Date This Action Is Released in PD2']").val();
        if (drop ==="Complete" && date==="")
        {
            alert("The Date This Action Is Released in PD2 is required due to the selection of the Stage equaling Complete. Please enter a date for Date This Action Is Released in PD2");
            return false;
        }   
        else
        {
            return true;
        }
    }
     </script>
    

    Then when Stage is Complete, if the date field not fill any value, it will pop up the warning validation and not save the item, please check the capture below:

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, June 14, 2019 6:09 AM
  • Hi Imossolle,

    Did the modified code work for you, please provide a update of this question if you need more help.

    And if my reply is helpful to this question, you could Mark as answer so that it could help others who stuck with the same question in the forum.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by lmossolle Monday, November 25, 2019 6:03 PM
    Wednesday, June 19, 2019 8:53 AM

All replies

  • Hi,

    Try providing the jquery-1.12.4.min.js in style library and provide the Url from there just like below.

    <script src="
    https://global.sharepoint.com/:u:/r/sites/treys/Style%20Library/jquery-1.12.4.min.js"
           type="text/javascript"></script>

    <script type="text/javascript">
     function PreSaveAction() {
    drop = $("select[title='Stage']").val();
    date = $("input[title='Date This Action Is Released in PD2']").val();
        if (drop ==="Complete" && date===""){
            alert("The Date This Action Is Released in PD2 is required due to the selection of the Stage equaling Complete. Please enter a date for Date This Action Is Released in PD2");
            return false;
        }   
            return true;
    }
     </script>

    Thanks & Regards,


    sharath aluri

    • Marked as answer by lmossolle Monday, November 25, 2019 6:03 PM
    Thursday, June 13, 2019 5:18 PM
  • I have tried the above. However it is not working. Can you relook at this?

    Lee Mossolle

    Thursday, June 13, 2019 6:30 PM
  • Hi Im,

    Below article is the same kind of requirement they have other than just changing fields according to your requirement.

    https://social.msdn.microsoft.com/Forums/office/en-US/481f4f90-5d65-44de-a408-5e1dc3017ffb/how-to-make-another-column-in-a-list-as-mandatory-based-on-choice-column-value-in-sp-2010-custom?forum=sharepointcustomizationprevious

    Thanks & Regards,


    sharath aluri


    Thursday, June 13, 2019 11:35 PM
  • The other and easiest way of doing without Jquery is: Go to List -> List Settings -> Validation Settings apply the formula that i provided in the down. I changed the formula as per your requirement:

    Formula:

    =IF(Stage="Complete",IF([Date This Action Is Released in PD2]<>"",TRUE,FALSE),TRUE)=IF(Stage="Canceled",IF([Date This Action Is Released in PD2]<>"",TRUE,FALSE),TRUE)

    Thanks & Regards,


    sharath aluri

    • Marked as answer by lmossolle Monday, November 25, 2019 6:03 PM
    Friday, June 14, 2019 12:33 AM
  • Hi Imossolle,

    Modified the code snippet as below:

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
     function PreSaveAction() {
    drop = $("select[title='Stage']").val();
    date = $("input[title='Date This Action Is Released in PD2']").val();
        if (drop ==="Complete" && date==="")
        {
            alert("The Date This Action Is Released in PD2 is required due to the selection of the Stage equaling Complete. Please enter a date for Date This Action Is Released in PD2");
            return false;
        }   
        else
        {
            return true;
        }
    }
     </script>
    

    Then when Stage is Complete, if the date field not fill any value, it will pop up the warning validation and not save the item, please check the capture below:

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, June 14, 2019 6:09 AM
  • The above works, but if the stage was canceled how could you make that a requirement also? As far as having a stage for complete and canceled? Thanks for the assist!

    Lee Mossolle

    Friday, June 14, 2019 2:53 PM
  • Hi,

    Modify the code snippet as below to add validation to Canceled stage:

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    
    <script type="text/javascript">
     function PreSaveAction() {
    drop = $("select[title='Stage']").val();
    date = $("input[title='Date This Action Is Released in PD2']").val();
        if (drop ==="Complete"&& date==="")
        {
            alert("The Date This Action Is Released in PD2 is required due to the selection of the Stage equaling Complete. Please enter a date for Date This Action Is Released in PD2");
            return false;
          
        }   
        else if(drop ==="Complete"&& date!="")
        {
            return true;
        }
        else if(drop ==="Canceled")
        {
          alert("Stage is Canceled");
          return true;
        }
    }
     </script>
    

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, June 17, 2019 5:40 AM
  • Hi Imossolle,

    Did the modified code work for you, please provide a update of this question if you need more help.

    And if my reply is helpful to this question, you could Mark as answer so that it could help others who stuck with the same question in the forum.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by lmossolle Monday, November 25, 2019 6:03 PM
    Wednesday, June 19, 2019 8:53 AM