0

I have a dropdown named "Status" with a "Yes" and "No With Reservation" as options.

If the user choose "Yes", form can be submitted, if "No With Reservation", user must fill out an "Explanation" multiline textbox below the dropdown before a form can be submitted.

I have access to SharePoint Designer so any help to achieve this is highly appreciated.

I am using the OTB built in validation and entering the following formula to validation within the SharePoint List Settings.

This one doesn't work because it doesn't recognize "No with Reservation" which is a must but if I change it to just "No" and use a single Explanation textbox instead of multiline, it works.

=IF([Status]=”No with Reservation”, IF([Explanation]<>””,TRUE,FALSE),TRUE)

2 Answers 2

0

As per you comment to Nisarg's answer your best bet may be a simple piece of Javascript.

In a script web part or a file attached to a content editor web part:

(If you already have jquery on the form/or on site change the src call as needed. Current one below requires an outside internet connection to work)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
function PreSaveAction()
{
    return formVal(); //calls the validation on save
}
function formVal() {
    if ($("[title='NameOfChoiceField']").val() == 'Yes') {
        return true;
    }
    else {
        if ($("[title='NameOfTextBoxField']").val().length) //checks if not empty use length > X to stipulate min char length allowed
        {
            return true;
        }
        else { //textbox is empty
            alert('Error: Please add a reason into the box before submitting');
        }
    }

    return false;
}
</script>

Edited as per Danny's suggestion regarding PreSaveAction()


From here are edits made by user normsp

Update: I had to add more dropdowns and multiline textboxes but something is wrong with my "OR" syntax. And also, is it possible to add a separate error message for each multiline textboxes?

<script>
function formVal() {
var errorMessage = ''; //All error messages will be displayed in one alert box at end of check
//a '!' indicates not equal to
if ($("[title='Acknowledgement']").val() != 'Yes')
{
    if ($("[title='Acknowledgement Exception']").val().length < 0)
    {
        errorMessage += 'Error Message1 \n';

    }
    //add other if's here to check for other conditions. Alternatively use a switch statement
}
if ($("[title='Preparation/Review']").val() != 'Yes') {
    if ($("[title='Preparation/Review']").val().length < 0) {
        errorMessage += 'Error Message2 \n';

    }
}

/*
add other field checks here
*/


if (errorMessage)//checks if errorMessage is not empty
{
    alert(errorMessage);
    return false //Does not save.
}

return true;//if all validation passes return true to submit/save

}

7
  • 1
    Before you start messing with Button events, look into the PreSaveAction Apr 8, 2016 at 5:49
  • Thanks Danny, somehow learning about PreSaveAction passed me by.
    – Roland
    Apr 8, 2016 at 6:03
  • Roland, your post is fantastic. It is working but I failed miserable in adding more dropdowns and multiline textbox. My syntax is wrong if ($("[title='NameOfChoiceField']").val() == 'Yes') || if ($("[title='NameOfChoiceField2']").val() == 'Yes') { return true; }
    – normsp
    Apr 8, 2016 at 14:09
  • @normsp your if statements are wrong. You have to write them in this manner: if(x==y || a == y) instead you currently have it as if(x || a == y). This check if x exists or if a is equal to y, which is something you don't want
    – Roland
    Apr 10, 2016 at 23:11
  • @normsp see last code block for updated code.
    – Roland
    Apr 10, 2016 at 23:18
0

You should probably be able to use this:

=IF([Status]!='Yes',IF([Explanation]<>””,TRUE,FALSE),TRUE)

I think comparisons are not supported with multiline text. So your Explanation should be a single line column. (Not 100% sure on this.)

Also, if the comparison [Explanation]<>"" doesn't work, you could also try COUNTA([Explanation])>0, which just verifies that the field Explation is not empty and does not contain only the white spaces.

For greater customization you could look to ItemAdding event receivers.

3
  • =IF([Status]!='Yes',IF([Explanation]<>””,TRUE,FALSE),TRUE) I am getting syntax error so I changed it to =IF([Status]<>"Yes",IF([Explanation]<>””,TRUE,FALSE),TRUE) but now I am getting this error "One or more column references are not allowed, because the columns are defined as a data type that is not supported in formulas." because of the multiline textbox which is required.
    – normsp
    Apr 8, 2016 at 4:51
  • Right. Can you not switch the Explanation to single line text? Apr 8, 2016 at 5:44
  • I would like to for validation simplification but multiline textbox is required. Roland's reply below is what I am looking for. Thank you for your input.
    – normsp
    Apr 8, 2016 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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