Validators

C

Carlos A.

Hello all
I'm using validators for e-mail, date, etc.
How can I use a validator to specify the number of characters in a Texbox?
I need that the TextBox only accepts 10 chars.

Thanks in advanced.
 
H

Hans Kesting

Hello all
I'm using validators for e-mail, date, etc.
How can I use a validator to specify the number of characters in a Texbox?
I need that the TextBox only accepts 10 chars.

Thanks in advanced.

RegularExpressionValidator, with ValidationExpression ".{10}" will
accept any 10 characters (not more, not less)

Hans Kesting
 
M

mocsoft

The regular expression is no doubt the way to go, but if you wanted to
be fancy about it you could use a custom validator with an associated
class in your app_code folder. It looks like this:

This would be your aspx page:

****************************************************************************************************
<%@ Page Language="VB" %>
<%@ Register TagPrefix="custom" Namespace="myControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<custom:LengthValidator ID="CustomValidator1"
controltovalidate="TextBox1" MaximumLength="10" runat="server"
text="Less than 10" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
****************************************************************************************************



This would be your application code placed in the app_code folder:

****************************************************************************************************

Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace myControls
'''<summary>
'''Validates the length of an input field
'''</summary>
Public Class LengthValidator
Inherits BaseValidator

Dim _maximumLength As Integer = 0

Public Property MaximumLength() As Integer
Get
Return _maximumLength
End Get
Set(ByVal value As Integer)
_maximumLength = value
End Set
End Property

Protected Overrides Function EvaluateIsValid() As Boolean
Dim value As String =
Me.GetControlValidationValue(Me.ControlToValidate)
If value.Length > _maximumLength Then
Return False
Else
Return True
End If
End Function

End Class
End Namespace

****************************************************************************************************


Any trouble with this implementation let me know. Its probably a bit of
overkill but can easily be customized.
 
C

Carlos A.

....
My trouble is that I use a textbox with a Date value.
I have a validator that detects the wrong Date format but I work with
dd/MM/yyyy format. But people can enter also dd/MM/yy and it's accepted by
the validator.
I need that the date textbox be validated (it works well) but also accept
only 10 characters.
There is a way to put an expression that only accepted this format?
dd/MM/yyyy

Thanks
 
H

Hans Kesting

...
My trouble is that I use a textbox with a Date value.
I have a validator that detects the wrong Date format but I work with
dd/MM/yyyy format. But people can enter also dd/MM/yy and it's accepted by
the validator.
I need that the date textbox be validated (it works well) but also accept
only 10 characters.
There is a way to put an expression that only accepted this format?
dd/MM/yyyy

Thanks

RegularExpression: [0-3][0-9]/[01][0-9]/(19|20)[0-9]{2}
This will force the user to use two-digit day and month and four-digit
year.

Note: you will still need to test if it's a "real" date, as this
accepts "dates" like 39/19/2099.

Hans Kesting
 
M

mocsoft

It may take a little more thought but a better solution would be to
accept many different formats of dates and intelligently convert them
accordingly. Or to completely remove any complications use the calendar
control (various size options should you need them).

If you wanted to intelligently convert the following date for example,
08-dec-2006 you could run various checks on the input string. Such as
if substring(3,6) is in a pre-stored array of months. If you implement
something like this i would be interested to know how you got on.

Hans said:
...
My trouble is that I use a textbox with a Date value.
I have a validator that detects the wrong Date format but I work with
dd/MM/yyyy format. But people can enter also dd/MM/yy and it's accepted by
the validator.
I need that the date textbox be validated (it works well) but also accept
only 10 characters.
There is a way to put an expression that only accepted this format?
dd/MM/yyyy

Thanks

RegularExpression: [0-3][0-9]/[01][0-9]/(19|20)[0-9]{2}
This will force the user to use two-digit day and month and four-digit
year.

Note: you will still need to test if it's a "real" date, as this
accepts "dates" like 39/19/2099.

Hans Kesting
 
H

Hans Kesting

It may take a little more thought but a better solution would be to
accept many different formats of dates and intelligently convert them
accordingly.

How would you then decode "01.02.03" ?
- jan 2nd, 2003 (or 1903?) (as in mm.dd.yy)
- feb 1st, 2003 (dd.mm.yy)
- feb 3rd, 2001 (yy.mm.dd)
to use the common formats.

Hans Kesting
 
M

mocsoft

Well obviously there are restrictions that even the most gifted
programmer could not solve, so if that were to be the input then a
validation routine would ask the user which interpretation they meant.
This is obviously alot easier said than done but its certainly possible
and I would say once it was working many web developers would make use
of it.

This has actually got me to thinking, if I get some time I will try
some implementations of this, if your interested I could send you on
whatever I manage to do with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,063
Latest member
StormyShuf

Latest Threads

Top