DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Validation Function / Class

This is a discussion on Validation Function / Class within the .Net Development forums, part of the Programming & Scripting category; Does anyone use a standard class or function to provide validation in their apps? In a previous app I wrote ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

  #1  
Old November 16th, 2009, 07:36 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default 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.
__________________
Join the folding team
Reply With Quote
  #2  
Old November 16th, 2009, 08:19 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 563
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

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.
__________________
Get the Mantra!
Reply With Quote
  #3  
Old November 16th, 2009, 08:22 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

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?
Reply With Quote
  #4  
Old November 16th, 2009, 08:26 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 563
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

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

Comments on this post
richyrich agrees: Thanked Post
Reply With Quote
The Following User Says Thank You to micky For This Useful Post:
richyrich (November 16th, 2009)
  #5  
Old November 16th, 2009, 08:38 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,915
Blog Entries: 7
Rep Power: 13
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

I only use the validation controls. I have a lot of fun with the RegularExpressionValidator.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #6  
Old November 16th, 2009, 08:46 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,312
Blog Entries: 5
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

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?
Reply With Quote
  #7  
Old November 16th, 2009, 09:01 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,915
Blog Entries: 7
Rep Power: 13
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

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.

Comments on this post
richyrich agrees: Thanked Post
Reply With Quote
The Following User Says Thank You to jmurrayhead For This Useful Post:
richyrich (November 16th, 2009)
  #8  
Old November 16th, 2009, 09:07 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 563
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

But you would still need server side validation...... right??
Reply With Quote
  #9  
Old November 16th, 2009, 09:10 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,915
Blog Entries: 7
Rep Power: 13
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

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
Reply With Quote
  #10  
Old November 16th, 2009, 09:14 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 563
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

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

Lot of code would have been saved by me
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Tags
controls, function, validation

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Email Class jmurrayhead .Net Code Samples 2 July 8th, 2009 07:49 AM
Button Class richyrich HTML & CSS Help 18 February 27th, 2009 12:32 PM
class name is ambiguous richyrich .Net Development 11 August 12th, 2008 11:11 AM
FTP Class Library? Wolffy .Net Development 6 May 30th, 2008 10:26 AM
Class library Shem .Net Development 11 May 22nd, 2008 07:01 AM


All times are GMT -4. The time now is 02:04 AM.


Copyright ©2008-2009, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2