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; Originally Posted by jmurrayhead Code: <asp:TextBox ID="TextBox1" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="TextBox1 is required" Text="*" /> <asp:RegularExpressionValidator ...

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

  #11  
Old November 16th, 2009, 09:15 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,342
Blog Entries: 6
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
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.
So, in the case that textbox1 was blank, just the TextBox1 is required text would show and in the case someone entered 12345, just TextBox1 contains invalid characters would show?

And, in the example I gave, you could change <asp: panel> control to ValidationSummary and all error messages for all validation controls would show here?
__________________
Join the folding team
Reply With Quote
  #12  
Old November 16th, 2009, 09:16 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,342
Blog Entries: 6
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
Nice.
So then server side validation can be by-passed!!

Lot of code would have been saved by me
m, I think using validation controls is server side validation. They are .NET controls, after all.
Reply With Quote
  #13  
Old November 16th, 2009, 09:20 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
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 ofjmurrayhead has much to be proud of
Default

Quote:
Originally Posted by micky View Post
Nice.
So then server side validation can be by-passed!!

Lot of code would have been saved by me
Indeed

Quote:
Originally Posted by richyrich View Post
So, in the case that textbox1 was blank, just the TextBox1 is required text would show and in the case someone entered 12345, just TextBox1 contains invalid characters would show?

And, in the example I gave, you could change <asp: panel> control to ValidationSummary and all error messages for all validation controls would show here?
Yes, Yes and Yes!!

Quote:
Originally Posted by richyrich View Post
m, I think using validation controls is server side validation. They are .NET controls, after all.
As long as you perform the validation check using Page.IsValid as I have shown
__________________
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
  #14  
Old November 16th, 2009, 09:23 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

Quote:
Originally Posted by richyrich View Post
So, in the case that textbox1 was blank, just the TextBox1 is required text would show and in the case someone entered 12345, just TextBox1 contains invalid characters would show?
Yes, you'll need separate required validators and regular validator for 1 control.
And each for other controls that you want to validate.

Quote:
Originally Posted by richyrich View Post
And, in the example I gave, you could change <asp: panel> control to ValidationSummary and all error messages for all validation controls would show here?
ValidationSummary can show a message box or also can post the error like on a label

Quote:
Originally Posted by richyrich View Post
m, I think using validation controls is server side validation. They are .NET controls, after all.
I think they use javascript to fire, so if the js is disables, then they might not fire and hence the need for server side validation.
__________________
Get the Mantra!
Reply With Quote
  #15  
Old November 16th, 2009, 09:28 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
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 ofjmurrayhead has much to be proud of
Default

Quote:
Originally Posted by micky View Post
I think they use javascript to fire, so if the js is disables, then they might not fire and hence the need for server side validation.
They use JavaScript to fire the client-side validation, but using Page.IsValid on the server will fire the server-side validation that is defined by each validation control:
Code:
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
    If Page.IsValid Then
        ' Code to save the record here
    End If
End Sub

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

thats good to know J
Reply With Quote
  #17  
Old November 16th, 2009, 09:58 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,342
Blog Entries: 6
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

Would it be able to handle the scenario where you might have two submit controls where one submits the form in full and the other only checks a few of the textboxes?

Say, for example, you have a form that has name, date of birth and address details and a button that prepopulates a series of address textboxes if the user just enters a postcode / zipcode.

So, you want to validate the postcode, zipcode is valid when prepopulating the address, but you don't want to validate the name and date of birth fields until the full form is submitted.

How would you handle this using validation controls? I presume if you used Page.IsValid in the GetAddress function, it would pick up the name and date of birth validations as well.
Reply With Quote
  #18  
Old November 16th, 2009, 10:05 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,962
Blog Entries: 8
Rep Power: 15
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 ofjmurrayhead has much to be proud of
Default

Validation controls have a property called "ValidationGroup" where you would set the name (i.e. ValidationGroup="SaveRecord"). You would have to assign the ValidationGroup property of the individual buttons, using the same name as the form items they are to validate.

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)
  #19  
Old November 16th, 2009, 12:59 PM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,342
Blog Entries: 6
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

Just in case anyone comes across this thread for information, I had to change the validation expression to below:-
Code:
                    <asp:RegularExpressionValidator ID="revForename" runat="server" ControlToValidate="txtForename"
                        ErrorMessage="The forename you entered contains invalid characters" ValidationExpression="^[a-zA-Z ]+$"
                        Display="None" EnableClientScript="true" />
Otherwise it wouldn't accept the text I entered.
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 06:21 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2