Validation on SharePoint forms

In some cases there is a business requirement where you need a mendatory column even if it isn’t as a mendatory column defined. Therefore you can use the standard .Net validation.

  • Open SharePoint Designer and create a new form.
  • Go to the place where you need an additional validation and disable the SharePoint control.
    Create a new .Net control by using asp:Textbox.
    Please keep in mind that your control should use the same ID. Use “__designer:bind” as well.
    For a better understanding what’s going on with the ddwrt:DataBind visit this site (http://www.bryancook.net/2009/09/understanding-sharepoints-ddwrtdatabind.html).
  • change the PropertyName and eventName from ‘Value’,’ValueChanged’ to ‘Text’,’TextChanged’
  • Add JavaScript Event

<!– New control –>
<span id=”suvNr”><asp:TextBox onkeydown=”return isNumberKey(event);”  runat=”server” id=”ff1{$Pos}” __designer:bind=”{ddwrt:DataBind(‘i’,concat(‘ff1’,$Pos),‘Text’,‘TextChanged’,’ID’,ddwrt:EscapeDelims(string(@ID)),’@Suppliernumber’)}” type=”number”/>
< /span><span id=”suvNrValidate”></span>

< !– Disable the SharePoint control: –>
<!–<SharePoint:FormField runat=”server” id=”ff1{$Pos}” ControlMode=”New” FieldName=”Suppliernumber” __designer:bind=”{ddwrt:DataBind(‘i’,concat(‘ff1′,$Pos),’Value’,’ValueChanged’,’ID’,ddwrt:EscapeDelims(string(@ID)),’@Suppliernumber’)}”/>
–>

  • disable the SharePoint Save Buttons and insert new one

<!– Original SharePoint Save Button must be disabled –>
<!– <SharePoint:SaveButton runat=”server” ControlMode=”New” id=”savebutton1″/>–>
<!– new Button Control –>
<input name=”Submit” type=”submit” value=”Submit” style=”font-size: 11px; width: 126px;” onclick=”javascript: if (!PreSaveAction()) return false; {ddwrt:GenFireServerEvent(‘__commit;__redirect={/sitedirectory/mm/SitePages/Home.aspx}’)}”/>

  • Add jQuery Script

<script type=”text/ecmascript” src=”http://code.jquery.com/jquery-1.11.1.js”></script>

  • Add Script

<script>
/* Disable the save control from the ribbon */
$( document ).ready(function() {
$( “a:contains(‘Save’)” ).css( “visibility”, “hidden” );
});

/* allows only characters*/
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode > 105 || charCode < 96))
{
alert(“Only characters are allowed!”);
return false;
}
return true;
}

/* Do actions before save form */
function PreSaveAction()
{
var valueSUV = $(“span#suvNr input”).val();
if(valueSUV.length > 0)
{
$( “span#suvNrValidate” ).text( “” );
return true;
}
$( “span#suvNrValidate” ).text( “You must specify a value for this required field.” );
$( “span#suvNrValidate” ).css({“color”:”red”,”padding-left”:”3px”});
}
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *