none
I have three checkboxes that are validated to make sure at least ONE is selected when the user submits. RRS feed

  • Question

  • Hi All,

    I have three checkboxes that are validated to make sure at least ONE is selected when the user submits.

    This is what I would like to do:

    don't make any validation on 1st check box

    If the 2nd is checked, I have a description text box that I would like to become mandatory.

    If the 3rd is checked, I have a description text box that I would like to become mandatory.

    If 2nd and 3rd are both checked, I have a description text box that I would like to become mandatory.

    Here is the the validation script I have used to make sure that at least one of those CHECKBOXES has been checked.

    I want to implement the same in jquery.

    Tuesday, January 27, 2015 5:38 PM

Answers

  • To accomplish this in SharePoint, I recommend using list validation settings (List Settings -> Validation Settings) rather than jQuery.

    To make one column required depending on the value of another column, you can use a formula like this:

    =IF([My Checkbox Column],NOT(ISBLANK([My Required Field])),TRUE)

    To add more validation rules, you can use the AND() function to join multiple conditions.

    =AND([first formula],[second formula])

    =AND([first formula],AND([second formula],[third formula]))

    This could get to be a pretty ugly formula depending on how complex the validations need to be. If it gets too long, you can offload some of the complexity into one or two calculated columns, then reference and compare the calculated columns directly in the validation formula.

    For the example you describe, something like this should work:

    =AND(IF([1st Checkbox],NOT(ISBLANK([1st Textbox])),true), AND(IF([2nd Checkbox],NOT(ISBLANK([2nd Textbox])),true),IF(AND([1st Checkbox],[2nd Checkbox]),NOT(ISBLANK([3rd Textbox])),true)))

    Replace [1st Checkbox] and [2nd Checkbox] with the names of your two yes/no fields, and replace [1st Textbox], [2nd Textbox], and [3rd Textbox] with the names of your three textbox fields. Keep in mind that you'll need to make your "User Message" generic enough to handle any of the three things that might have been wrong with the form input.

    If the list validation settings don't work for the validation you need to perform, you can use JavaScript validation by overwriting the PreSaveAction() function on the edit form.

    Add a content editor web part with a link to a text file containing the following:

    <script>
    function PreSaveAction(){
         var isValid = true;
         /* Validation Logic to set isValid to false if something is wrong.*/
         if(!isValid){alert("Invalid input.");}
         return isValid;
    }
    </script> 

    I hope that helps!

    Tuesday, January 27, 2015 9:41 PM