0

I have 3 dropdowns with 3 related textboxes combo so if dropdown 1 is <> 'Yes' then textbox 1 must not be empty, if dropdown 2 is <> 'Yes' then textbox 2 must not be empty and so on. In addition, am I using the correct .js version? Any help is highly appreciated...

<script src="/Style%20Library/JS/jquery.min.js"></script>
    <script>
    function PreSaveAction()
    {
        return formVal(); //calls the validation on save
    }
    function formVal() {
        var dd1 = $("[title='dropdown 1']").val(), // a dropdown with values YES or No (with exception)
            txtbx1 = $("[title='textbox 1']").val(), // plain textbox that must be filled if dropdown No (with exception)

            dd2 = $("[title='dropdown 2']").val(),
            txtbx2 = $("[title='textbox 2']").val(),

        dd3 = $("[title='dropdown 3']").val(),
            txtbx3 = $("[title='dropdown 3']").val()
          valid = false;

        if(dd1 == 'Yes' && txtbx1 == '') {
            return true; // return true if dd1 = Yes and txtbx1 = empty
        }
            if(dd1 == 'No (with exception)' && txtbox1 !== '') {


    if(valid){
    return true;
    }
// return true if dd1 = No (with exception) and txtbx1 <> empty
        }

    // it is fine up to here but when conditional statements below are added, none of the conditional statements are working including the top of this line

            if(dd2 == 'Yes' && txtbx2 =='') {
            if(valid){
    return true;
    }
        }
            if(dd2 == 'No (with exception)' && txtbox2 !== '') {
            return true; // return true if dd2 = No (with exception) and txtbx2 <> empty
        }
        if(dd3 == 'Yes' && txtbx3 =='') {
           if(valid){
    return true;
    }
        }
            if(dd3 == 'No (with exception)' && txtbox3 !== '') {
            if(valid){
    return true;
    } // return true if dd3 = No (with exception) and txtbx3 <> empty
        }
        alert('Error: Please enter the required respective textbox message before submitting');
        return false;
    }
    </script>
  • You should use comments to ask for something extra. Editing your question does not do anything ( i don't get an alert, it was pure luck I check this question again and noticed your OP changed). – Roland Apr 19 '16 at 1:42
  • As explained here in a similar of your questions make you code simpler; it is easier to debug then. The code you posted above is missing a comma – Danny '365CSI' Engelman Apr 19 '16 at 8:34
0

Your returning true after each check.

A return means that you leave your code which means that the first time you hit a return no more code is used.

What you want to do is dirty bit handling. Make a new var valid and set to false. Replace the return true statements with valid = true.

at the bottom have an

 if(valid){
return true;
}
 alert(message);
return false;

UPDATE: Code example

var dd1 = $("[title='dropdown 1']").val(); // a dropdown with values YES or No (with exception)
    var txtbx1 = $("[title='textbox 1']").val(); // plain textbox that must be filled if dropdown No (with exception)

    var valid = true;

    if (dd1 == 'Yes' && txtbx1 == '') {
        valid = false; // return true if dd1 = Yes and txtbx1 = empty
    }
    /*
    The rest of your logic should follow.
    Check if something FAILS validation. IF so set valid to false
    */

    //if valid is still true passes validation
    if (valid) {
        return true;
    }
    //if it is not true
    //Fails validation and form is not saved
    alert('Error: Please enter the required respective textbox message before submitting');
    return false;

*********** My updates (normsp stuff)*********

var dd1 = $("[title='Responsibility']").val(), // a dropdown with values YES or No (with exception)
    txtbx1 = $("[title='RESP Exception']").val(), // plain textbox that must be filled if dropdown No (with exception)

    dd2 = $("[title='General Ledger']").val(),
    txtbx2 = $("[title='GL Exception']").val(),

dd3 = $("[title='Payroll Review']").val(),
    txtbx3 = $("[title='PR Exception']").val();
var valid =true;

if(dd1 == 'Yes' && txtbx1 == '') {
    valid= false; // **return true if dd1 = Yes and txtbx1 = empty**
}
    if(dd1 == 'No (with exception)' && txtbx1 !== '') {
    valid= false; // ***NOT VALIDATING*** **return true if dd1 = No (with exception) and txtbx1 <> empty**
}

if(dd1 == 'No (with exception)' && txtbx1 == '') {
    valid= false; // ***NOT VALIDATING*** **return true if dd1 = No (with exception) and txtbx1 = empty**
}

if (valid) {
return true;
}
alert('Error: Please enter the required respective textbox message before submitting');
    return false;
}

************ Finally - Working code *******

function formVal() {
var dd1 = $("[title='Responsibility']").val(), 
txtbx1 = $("[title='Responsibility Exception']").val(), 

dd2 = $("[title='General Ledger']").val(),
txtbx2 = $("[title='GL Exception']").val(),

dd3 = $("[title='dropdown 3']").val(),
txtbx3 = $("[title='dropdown 3']").val();

var valid = true;

if(dd1 == 'No (with exception)' && txtbx1 =='') {
valid=false;  
}

if(dd2 == 'No (with exception)' && txtbx2 =='') {
valid=false; 
}

if (valid) {
return true;

}
alert('Error: Please enter the required textbox message before submitting');
return false;
}
</script>
|improve this answer|||||
  • what did I missed?am using a wrong operator? see note ***NOT VALIDATING**** – normsp Apr 19 '16 at 15:10
  • @normsp looks like i messed up the logic. I am setting it to fail when it is meant to return true. Change the IF statements to check for the conditions that it fails for. I.e: if(= YES & txtbx1 != '') – Roland Apr 19 '16 at 23:26
  • Thanks for this. It helped a lot. At the end, I really don't need to check the "Yes" option - a yes is a yes so I am just checking the "No (with exception)" and required textbox. Right now it is working when it is a plain multiline text but not validating when I changed it to a rich textbox due to the fact there is a code getting inserted <div></div> in the rich textbox so how do I check a rich multiline textbox for null value? I think I have to use trim it but not sure where to add it. – normsp Apr 20 '16 at 15:31
  • @normsp there is a couple of different ways to do this. Best things is for you to open a new question at stackoverflow.com (the QA place for general code questions). You will want to textbox code and your current JS code in your question there – Roland Apr 20 '16 at 23:19

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.