1

I have a SharePoint 2013 Custom list. I want to do custom validation WITHOUT making the SharePoint list columns required.

Simply put, when a user selects a certain option from a dropdown, the next few fields need to become required.

I am trying to disable the SAVE button IF [choice] column = [specific value] AND [column 1] = blank AND [column 2] = blank AND [column 3] = blank AND [column 4] = blank

Any ideas?? Here is the code I am fooling around with:

//Disable Save Button based on Blanks
$("select[title='Website']").change(function() {
    if ($("select[title='Website']").val() == "")) {
        $("input[value$='Save']").attr('disabled', true);
    } else {
        $("input[value$='Save']").attr('disabled', false);      
                }
2

Rather than enabling/disabling the Save button, what I would do is add a click handler to the Save button, and when the user clicks Save, check for your conditions (value of drop-down, then the subsequent fields if necessary). If the conditions aren't met, display an error message and prevent the Save click from continuing to do the post-back.

This is just off the top of my head, not tested, so you might have to play around with it to get it working exactly right, but something like:

$('input[value="Save"]').click(function(e){
    var validForm = true;

    if($('select[title="Website"]').val() === "Specific Value") {
        var firstVal = $('input[title="Column1"]').val();
        var secondVal = $('input[title="Column2"]').val();
        var thirdVal = $('input[title="Column3"]').val();
        if (!(firstVal && secondVal && thirdVal)){
            validForm = false;
        }
    }

    if (!validForm){
        alert('You must provide values for Column 1 2 and 3');
        $(e).preventDefault();
        return false;
    } else {
        return true;
    }
});
  • +1, or use PreSaveAction instead of binding to the button. – Eric Alexander Jan 5 '17 at 17:32
  • hi Dylan - thanks for the reply. Playing around with your idea and it looks like it works. – Jake M. Jan 5 '17 at 18:16
  • However - the 1 thing is: even after it shoots off the Alert that they did not complete the necessary fields....The sharepoint save action still goes through and the form submits. Any way to prevent this? – Jake M. Jan 5 '17 at 18:17
  • I would google for "PreSaveAction" as @EricAlexander suggested, and see if you can figure out how to get that working. That will most likely work since PreSaveAction is a tool SharePoint provides for doing exactly what you want to do. Here's something I found quickly - using PreSaveAction. – Dylan Cristy Jan 5 '17 at 18:21

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.