Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 2nd, 2010, 10:34 AM
Authorized User
 
Join Date: Jul 2009
Posts: 61
Thanks: 15
Thanked 1 Time in 1 Post
Default

I really appreciate all the help I am getting on here. And I apologise if I am asking really basic questions.

This is where I am at.
My Function on the code behind page now looks like this:
Code:
Protected Sub ValidateText10(ByVal sender As Object, ByVal args As ServerValidateEventArgs)

        Dim numOfWords As String() = args.Value.Split(New Char() {" "c})
        ' args.IsValid = args.Value.Length <= 10
        ' args.IsValid = numofwords.Length <= 10
        args.IsValid = args.Value.Length < 11

    End Sub
The Validation now shows the Message Box correctly but if I enter anything in the text box it seems happy no matter what the length. I've tried several 'tests' as you can see but no matter how many characters I enter it does not complain.
I have not entered a MaxLength in the asp:ttextbox as I guess this would prevent the validation anyway by simply limiting the number of characters?
__________________
Geoff Baldwin
 
Old March 2nd, 2010, 11:04 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Exactly what is it you're trying to do -- limit the number of characters, or the number of words? You are not being very clear on that.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 2nd, 2010, 11:20 AM
Authorized User
 
Join Date: Jul 2009
Posts: 61
Thanks: 15
Thanked 1 Time in 1 Post
Default

Sorry --
I'm looking to limit the number of characters. I confused the issue by not checking some code I found that converted chars to words :(
__________________
Geoff Baldwin
 
Old March 2nd, 2010, 11:30 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
Default

Do you want to count the number of characters as you type or when you submit?
__________________
Try our latest project www.nobanx.com Currency Exchange for members
 
Old March 2nd, 2010, 11:35 AM
Authorized User
 
Join Date: Jul 2009
Posts: 61
Thanks: 15
Thanked 1 Time in 1 Post
Default

As I type would be a great bonus!
When I submit would be better than I have now!
__________________
Geoff Baldwin
 
Old March 2nd, 2010, 11:39 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Then just use this as the method:

Code:
Protected Sub ValidateText10(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)      
   args.IsValid = args.Value.Length <= 10
End Sub
Here is a full example with one textbox. Just slap the same CustomValidator on every textbox you want to apply this validation method to.

Code:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
   Protected Sub ValidateText10(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
      args.IsValid = args.Value.Length <= 10
   End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Custom Validator Sample</title>
</head>
<body>
   <form id="form1" runat="server">
      <p>
         <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" />
         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Business description is required."
            ControlToValidate="TextBox1" Display="Dynamic" />
         <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Business description too long."
            ControlToValidate="TextBox1" OnServerValidate="ValidateText10" Display="Dynamic" />
      </p>
      <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
   </form>
</body>
</html>
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 2nd, 2010, 12:31 PM
Authorized User
 
Join Date: Jul 2009
Posts: 61
Thanks: 15
Thanked 1 Time in 1 Post
Default

OK -- thanks for that.
I've implemented it and the validation works. That is, when I submit the page The error appears as a 'summary' rather than in the messsage box.
Presumably, this is because I am doing the validation on the server.
Is there any reason why I am -- other than my misleading everyone as a result of the example I found?
Presumably all my other validations (required field, phone format etc) are client side.
And what does display=dynamic do? I don't see any difference in behaviour whether I set Display to static,dynamic or not at all!
__________________
Geoff Baldwin
 
Old March 2nd, 2010, 12:41 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

The built in controls (like RequiredField) do the validation BOTH on the server and the client. When you use a CustomValidator, there is no "built-in" validation -- you have to write it yourself. You can write server-side validation, client-side validation, or both.

When using a CustomValidator, having a server-side validation function is a MUST. That is because client-side validation, while faster for the user, is very easy to defeat.

Now that you have the server-side working, you can add the client-side validation using JavaScript.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
max length property of multiline text box MunishBhatia ASP.NET 2.0 Professional 5 June 14th, 2007 01:55 AM
åäö Characters in Text Box itHighway Classic ASP Basics 1 August 5th, 2006 01:54 AM
put a limit on the number of characters entered in crmpicco HTML Code Clinic 6 April 12th, 2005 06:08 AM
VS.NET - Using a multiline text box VSnewbie Classic ASP Databases 3 September 10th, 2004 09:50 AM
Multiline Text in Rich TextBox Niaz Pro VB 6 3 March 1st, 2004 07:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.