+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19

Thread: Validation Function / Class

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Validation Function / Class

    Does anyone use a standard class or function to provide validation in their apps?

    In a previous app I wrote a validation function that took parameters of the value to validate, the max length it could be, whether it was required and a code to use a specific regex pattern to validate.

    So, I could reuse this function on all form controls to validate the content. My preference is to have an "error display box" on the page that is used to display any validation errors on a sequential, singular basis. For example, suppose you had the following page:-
    Code:
    <asp:panel id="pnlError" runat="server">
    </asp:panel>
    <asp:textbox id="textbox1" runat="server" /> //This element is required
    <asp:textbox id="textbox2" runat="server" /> //This element can only accept [a-z]
    
    If textbox1 is left blank and textbox2 contains 123456, all I want to show is that texbox1 must be completed. I think it makes it more confusing for the user if they have a whole list of validation errors to complete.

    I don't know if this is a particularly good or acceptable method of validating?

    I know .NET provides validation controls. Does anyone just use these or do you have a standard function that you reuse in apps?

    What method(s) do you guys tend to use?

    btw, I know about providing client side and server side validation. I'm referring just to server side here.

  2. #2
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,758
    Blog Entries
    2
    Rep Power
    8

    Generally i use different code for validation on each page.

    If there is some special validation like email, integer, etc...; i make their functions separately and just use them on each page.

  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by micky View Post
    Generally i use different code for validation on each page.

    If there is some special validation like email, integer, etc...; i make their functions separately and just use them on each page.
    Do you use the built in validation controls or do you write specific validation functions for each control you have?

    Could you just do a quick, simple example?

  4. #4
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,758
    Blog Entries
    2
    Rep Power
    8

    Sometimes i use the built in ones like IsDate, like
    Code:
    If strDate = String.Empty Or Not IsDate(strDate) Then
            sText = "&nbsp; Date!"
    End If
    
    Sometimes i use functions, like for checking float values
    Code:
    If Regex.IsMatch(strLongitude, CGlobal.CheckFloat) = False Then
            sText = "&nbsp; Longitude is in Wrong Format!"
    End If
    
    Code for CGlobal.CheckFloat
    Code:
    Public Shared Function CheckFloat() As String
        'Decimals value
        CheckFloat = "^(-)?[0-9]+(\.[0-9]{1,10})?$"
    End Function
    

  5. #5
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,533
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    I only use the validation controls. I have a lot of fun with the RegularExpressionValidator.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by jmurrayhead View Post
    I only use the validation controls. I have a lot of fun with the RegularExpressionValidator.
    Could you give an example J?

    Also, what about if you have a required field and one that should only contain [a-z], for example?

  7. #7
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,533
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Code:
    <asp:TextBox ID="TextBox1" runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
           ControlToValidate="TextBox1"
           Display="Dynamic"
           ErrorMessage="TextBox1 is required" 
           Text="*" />
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
           ControlToValidate="TextBox1"
           Display="Dynamic"
           ErrorMessage="TextBox1 contains invalid characters"
           Text="*"
           ValidationExpression="^[a-zA-Z]$" />
    
    This requires the use of the ValidationSummary control to display all error messages in a box together.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  8. #8
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,758
    Blog Entries
    2
    Rep Power
    8

    But you would still need server side validation...... right??

  9. #9
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,533
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by micky View Post
    But you would still need server side validation...... right??
    Validation controls perform both client-side and server-side validation. All you need to do is add:
    Code:
    If Page.IsValid Then
        ' Place code here
    End If
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  10. #10
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,758
    Blog Entries
    2
    Rep Power
    8

    Nice.
    So then server side validation can be by-passed!!

    Lot of code would have been saved by me

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Email Class
    By jmurrayhead in forum .NET Code Samples
    Replies: 3
    Last Post: July 8th, 2011, 08:09 AM
  2. Button Class
    By richyrich in forum HTML & CSS Help
    Replies: 18
    Last Post: February 27th, 2009, 12:32 PM
  3. class name is ambiguous
    By richyrich in forum .NET Development
    Replies: 11
    Last Post: August 12th, 2008, 12:11 PM
  4. FTP Class Library?
    By Wolffy in forum .NET Development
    Replies: 6
    Last Post: May 30th, 2008, 11:26 AM
  5. Class library
    By Shem in forum .NET Development
    Replies: 11
    Last Post: May 22nd, 2008, 08:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO