sharepeoint list Newform.aspx needs to check before saving the item, for that i wrote a PreSaveAction funtion using javascript. right now i'm checking the expiration date with current date before saving the item and it looks like this

   function PreSaveAction() {
     var a1=......
    var  a2=.....
   if (a1<=a2){
return true;
}
else
{
window.location.href= "redirection.aspx";
return false;}

now i need to add something to this to check the attachments (to make the attachments required) to the newform.aspx for that

    function PreSaveAction() {

            var elm = document.getElementById("idAttachmentsTable");
            if (elm == null || elm.rows.length == 0)
{
                document.getElementById("idAttachmentsRow").style.display='none';

alert("Please attach Documents");
return false ;
}
else { return true ;}
}

i need to combine both the codes above in PreSaveAction function, so it should check the attachments first then check the expiration date.

Thanks in advance.

Try this:

function PreSaveAction() {
    var a1=......
    var a2=.....
    var elm = document.getElementById("idAttachmentsTable");
      if (elm == null || elm.rows.length == 0)
       {
        document.getElementById("idAttachmentsRow").style.display='none';
        alert("Please attach Documents");
        return false ;
       }
      else
       { 
        if (a1<=a2)
        {
         return true ;
        }
        else
        {
            window.location.href= "redirection.aspx";
            return false;
        }
       }
}

Though above answer is also correct and one way to do this...But I would have done below.. Ideal is to separate your validation logic in separate function and call it from PreSaveAction method. so in this way if in future you need to add more validation just create separate function and call it from PreSaveAction..

One thing to note in your code is that after "window.location.href" - your next statement of return false will never execute because redirection will already occur.

function PreSaveAction() {
    var returnValue = checkforattachment();

    if(returnValue){
        returnValue = checkforothercondition();
    }
    return returnValue;
}   

function checkforothercondition(){

 var a1=......
    var  a2=.....
   if (a1<=a2){
return true;
}
else
{
window.location.href= "redirection.aspx";
return false; // THIS WILL NEVER EXECUTE...BECAUSE REDIRECTION WILL HAPPEN BEFORE THAT.

}

function checkforattachment(){

var elm = document.getElementById("idAttachmentsTable");
            if (elm == null || elm.rows.length == 0)
{
                document.getElementById("idAttachmentsRow").style.display='none';

alert("Please attach Documents");
return false ;
}
else { return true ;}

}
   function PreSaveAction() {
      var a1=......
      var  a2=.....
      var elm = document.getElementById("idAttachmentsTable");



if (elm == null || elm.rows.length == 0)
    {

       document.getElementById("idAttachmentsRow").style.display='none';

       alert("Please attach Documents");
       return false ;
    }
    else     
   { 
        if (a1<=a2){
                    return true;
                    }
        else
                    {
                     window.location.href= "redirection.aspx";
                     return false;
                     }
    }
   }

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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