Adding Validation to a Custom NewForm.aspx in SharePoint & Redirect


SharePoint has a built in function called "PreSaveAction" that can be invoked to process anything before the form should submit. So now we will create a submit button which calls “PresaveActon” function.
Once users Submits the form, PreSaveAction is executed. If PreSaveAction is returns true it will commit and redirect. If the PreSaveAction returns false, then the commit will not happen nor the redirect.

Input Button Code:
Replace this code
<SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton1"/>
with

<input type="button" value="Submit" name="btnSave" onclick="javascript: if(PreSaveAction()) {ddwrt:GenFireServerEvent('__commit;__redirect={http://<Site>/ SitePages/ThankYou.aspx}')}" />

Add Script in NewCustom Form.
Now edit New custom newform.aspx and add content editor webpart and with below script

<script type="text/javascript" >
function PreSaveAction() {
var dateSelected = $("input[title$='Due Date']").val();
if(dateSelected == '' ||dateSelected == null ){
                alert("Please select Due Date");
                $("input[title$='Due Date']").focus();
                 return false;
}
 else   {
    return true;
  }
}
</script>


Popular posts from this blog

Adding Contact Selector Control to InfoPath form

How to add to Sub-Sub Menu in SharePoint