New Message on dotNET User Group Hyd

Regards to RegularExpressionValidator

Reply
  Reply to Sender   Recommend Message 3 in Discussion
From: AlwaysLakshmi

Hi,

You can write a regular _expression_ as shown in the example below:

' RegexDomValidator.vb
Option Explicit
Option Strict

Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Imports System.Drawing.Design
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


Namespace DomValidators
   <ToolboxData("<{0}:RegexDomValidator runat=server ErrorMessage=""RegexDomValidator""></{0}:RegexDomValidator>")> _
   Public Class RegexDomValidator
      Inherits BaseDomValidator

      <Bindable(True), _
         Category("Behavior"), _
         DefaultValue(""), _
         Editor("System.Web.UI.Design.WebControls.RegexTypeEditor,System.Design", _
         GetType(UITypeEditor)), _
         Description("ValidationExpression")> _
      Public Property ValidationExpression() As String
         Get
            Dim o As Object = ViewState("ValidationExpression")
            If o Is Nothing Then
               Return String.Empty
            Else
               Return CStr(o)
            End If
         End Get
         Set
            Try
              Regex.IsMatch("", value)
            Catch e As Exception
               'Throw new HttpException.
               '                       HttpRuntime.FormatResourceString(SR.Validator_bad_regex, value), e);
               Throw New HttpException("Bad _expression_", e)
            End Try
            ViewState("ValidationExpression") = value
         End Set
      End Property
     
      Protected Overrides Sub AddAttributesToRender(writer As HtmlTextWriter)
         MyBase.AddAttributesToRender(writer)
         If RenderUplevel Then
            writer.AddAttribute("evaluationfunction", "RegularExpressionValidatorEvaluateIsValid")
            If ValidationExpression.Length > 0 Then
               writer.AddAttribute("validationexpression", ValidationExpression)
            End If
          End If
      End Sub
     
      Protected Overrides Function EvaluateIsValid() As Boolean
         ' Always succeeds if input is empty or value was not found.
         Dim controlValue As String = GetControlValidationValue(ControlToValidate)
         Debug.Assert( Not (controlValue Is Nothing), "Should have already been checked")
         If controlValue Is Nothing Or controlValue.Length = 0 Then
            Return True
         End If
         Try
            ' Looking for an exact match, not just a search hit.
            Dim m As Match = Regex.Match(controlValue, ValidationExpression)
            Return m.Success And m.Index = 0 And m.Length = controlValue.Length
         Catch
         End Try
      End Function
   End Class
End Namespace

In the validator _expression_, you can check for the dd/mm/yyyy format as , the value in the textbox is compared with the value of the validator _expression_. In the above code, the validator _expression_ checks for empty and null spaces. you can code it to check for date value.

HTH

Regards

Lakshmi


View other groups in this category.

Click here!
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to