Posted By

nickaranz on 08/02/11


Tagged

validation controls 2007 Development sharepoint 2010


Versions (?)

SharePoint Field Validator


 / Published in: ASP
 

URL: http://karinebosch.wordpress.com/sharepoint-controls/sharepoint-validation-controls/

  1. <%@ Register TagPrefix="spuc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  2.  
  3. <spuc:InputFormRequiredFieldValidator ID="UserNameValidator" runat="server" Display="Dynamic" SetFocusOnError="true" ControlToValidate="TextBoxFromEmail" BreakBefore="true" ErrorMessage="Email is required" />
  4.  
  5. <spuc:InputFormRangeValidator ID="AgeValidator" runat="server" Display="Dynamic" SetFocusOnError="true"
  6. ControlToValidate="AgeInputFormTextBox" BreakBefore="true"
  7. Type="Integer" MinimumValue="21" MaximumValue="30"
  8. ErrorMessage="You must be between 21 and 30 years old." />
  9.  
  10. <spuc:InputFormRegularExpressionValidator ID="Validator" runat="server" Display="Dynamic" SetFocusOnError="true"
  11. ControlToValidate="EmailTextBox"
  12. ValidationExpression="^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
  13. ErrorMessage="Enter a valid email address." />
  14.  
  15. <spuc:InputFormCustomValidator ID="UsernameCustomValidator" runat="server" Display="Dynamic" SetFocusOnError="true"
  16. ControlToValidate="UsernameTextBox"
  17. OnServerValidate="ValidateUserName"
  18. ErrorMessage="Your user name must be at least 10 characters long (server-side validation)."
  19. ValidateEmptyText="true" />
  20.  
  21. In your code-behind you define the controls as protected class-level variables:
  22.  
  23. protected InputFormTextBox UsernameTextBox;
  24. protected InputFormCustomValidator UsernameCustomValidator;
  25. And you add the server-side validation method. This method accepts 2 incoming arguments: a source object being the control to validate, and a args object having properties as Value and IsValid. The Value property contains the value to validate. Set the IsValid property to true if the validation is successful or set the IsValid property to false if the validation fails.
  26.  
  27. protected void ValidateUserName(object source, ServerValidateEventArgs args)
  28. {
  29. if (args.Value.Length >= 10)
  30. args.IsValid = true;
  31. else
  32. args.IsValid = false;
  33. }

Report this snippet  

You need to login to post a comment.