1

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.

3 Answers 3

0

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; } } } 
0

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 ;} } 
0
 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; } } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.