0

I am having issue with the code for PreSaveAction on my SharePoint edit form. When the Attainment Type field equals Net new lines, I need to make the Actual Attainment field required.

I am using SP2013 Foundation. Attainment Type is a drop down field and Actual Attainment is a text field but it's numeric. The code below does not prevent the user from saving the Edit Form when the conditions exist.

Here is my code (I am using jquery-2.1.4.min.js & sputility.min.js):

    //form validation

    function PreSaveAction() {

    var attainment = SPUtility.GetSPField('Attainment Type');
    var attainmentValue = attainment.GetValue();
    var actual = SPUtility.GetSPField('Actual Attainment');
    var actualValue = actual.GetValue();

    if(attainmentValue=='Net new lines') {

        if(actualValue <= 0) {
        alert('Please enter the actual attainment at time of evalution.');
            return false;   
        }
        else {
            return true;
        }
    }
}
  • Also put an else{} field for if(attainmentValue =='Net new lines'). Function should return true if the attainmentValue is not net new lines. – Ankit Kumar Sep 2 '16 at 6:21
1

It could be SPUtility is seeing the value as a string perhaps. There also looks to be a typo in your function with your closures.

You are using jQuery, so there really isn't a need for SPUtility to get the data. Something like:

function PreSaveAction() {

  var attainmentValue = $("select[Title='Attainment Type']").val();
  var actualValue = $("input[Title='Actual Attainment']").val();
  console.log(typeof actualValue);

    if(attainmentValue=='Net new lines') {
      if(actualValue <= 0) {
        alert('Please enter the actual attainment at time of evalution.');
        return false;   
      }
    }
    else {
        return true;
    }
}

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.