Validation in ASP.NET
page 6 of 9
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36301/ 59

Custom Validation

Custom Validation

Creating your own validation isn't too hard, if you're no good a regular expressions then you can use this to do them. The following one makes sure that your entry is at least 8 letters long.

<script language="VB" runat="server">
Sub ValidateMe(sender as object, e as ServerValidateEventArgs)
     If Len(e.Value) > 8 Then
          e.IsValid = True
     Else
          e.IsValid = False
     End If
End Sub
</script>

Enter a username:<asp:textbox id="username" runat="server" /><p>

<asp:CustomValidator runat="server" OnServerValidate="ValidateMe" ControltoValidate="username" ErrorMessage="Username must be at least 8 letters long" />

Ok, the sub takes in the usual values, but it takes in ServerValidateEventArgs instead of EventArgs, this lets you get the value and set the IsValid Property. However this is server side validation (you'll learn more about why this is important soon), to make it client side, just add the property - "OnClientSideValidator="validating"". Then add some client side JavaScript to handle it (in the form of a function), otherwise it gets processed on the server.

But wait! There's more!

More? Yes! Here are some things that will add you your adventure's. Now obviously, having the Error text red all the time will get a bit annoying, so you can add the ForeColor = "Color" property to the control to change the error text color.

Now if you take all the code that I've given you on this page and put it into one file, add a submit button, form tags and then run it you will see that the Validation occurs with JavaScript and Span tags. This means that the page doesn't get submitted until the validators are satisfied with what you've put in. But you'll also see that there are gaps where the validators are on the page which are blank, they are activated when an error is found. You can set a Display property to change this -

  • Display = "None"    'No error message is displayed and the spaces are gone
  • Display = "Static"  'Default, spaces are left for the errors
  • Display = "Dynamic" 'Spaces are generated only if an error occurs.

You'll learn more about this in the next section.

When you submit the page, it doesn't go through until the validators are happy, when it is submitted, its checked again and a value - Page.IsValid is generated to tell you if all the validators are happy -

<script language="VB" runat="server" >
Sub Page_Load(sender as object, e as EventArgs)
If Page.IsPostBack Then
    If Page.IsValid Then
       lblMessage.Text = "You passed Validation"
    End If
End If
End Sub
</script>

<asp:label id="lblMessage" runat="server"/><p>

See?


View Entire Article

User Comments

Title: Nice   
Name: Nagesh
Date: 2004-07-13 9:49:31 AM
Comment:
Thanks for such quick review ....
Title: GOOD   
Name: NAGESH
Date: 2004-07-13 8:28:00 AM
Comment:
But where is the code!!!!!!!!!!!!!!!!!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 9:36:54 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search