Here is the user control's vb code behind file (NumberValidator1.ascx.vb)
'Written By: Glitch
#Region "Import Statements"
Imports System.IO
Imports System.EventArgs
#End Region
Partial Class Control_NumberValidator1
Inherits System.Web.UI.UserControl
#Region "General Declarations"
#Region "Public - General Declarations"
#End Region
#Region "Private - General Declarations"
'Set the Error Message for the Validation Control
Private _errorMessage As String
'Set the value of the textBox
Private _textValue As String
'Validation human string variable
Private _validExpression As String
'Validation variable.... accepts the value of _validEcpression
Private _validatorExpression As String
' Label Value assignment
Private _LabelMsg As String
' Label On/Off Switch
Private _TextSwitch As Boolean
' Variable to make the label visible or invisible
Private _LabelISeeU As Boolean
#End Region
#End Region
#Region "Constructor"
' Random Image Control Constructor
Public Sub New()
' Initialize & Normalize Random Image Variables
InitVars()
End Sub
#End Region
#Region "Properties"
#Region "Read/Write - Properties"
Public Property TextBoxValue() As String
Set(ByVal value As String)
_textValue = value
txtValidText.Text = _textValue
End Set
Get
_textValue = txtValidText.Text
Return _textValue
End Get
End Property
Public Property UpdateMode() As UpdatePanelUpdateMode
Get
Return Me.UpdatePanel1.UpdateMode
End Get
Set(ByVal value As UpdatePanelUpdateMode)
Me.UpdatePanel1.UpdateMode = value
End Set
End Property
#End Region
#Region "Read - Properties"
#End Region
#Region "Write - Properties"
Public WriteOnly Property SetValidationSummary() As String
Set(ByVal value As String)
If Len(Trim(value)) > 0 Then
' We trim down the value and remove any junk from the code
' We then assign it a value
_validExpression = Trim(value)
'If its text... set it to check for letters only; no special characters at all
If _validExpression = "text" Then
_validatorExpression = "\b[A-ZA-z]+\b"
_errorMessage = "Accepts Letters Only"
'If its number... set it to check for numbers only
ElseIf _validExpression = "number" Then
_validatorExpression = "^(\d|-)?(\d|,)*\.?\d*$"
_errorMessage = "Accepts Numbers Only"
'If its email... set it to check for a valid e-mail entry only
ElseIf _validExpression = "email" Then
_validatorExpression = "[\w]+@[\w]+\.(com|net|org|co\.th|go\.th|ac\.th|or\.th|go.\th)"
_errorMessage = "Accepts E-Mails Only"
'If its zip... set it to check for a zip code of the following format: 12345 or 12345-6789
ElseIf _validExpression = "zip" Then
_validatorExpression = "^(\d{5})(-\d{4})?$"
_errorMessage = "Accepts Zip Codes Only"
'If its phone... set it to check for a valid phone number only. accepts extensions also.
ElseIf _validExpression = "phone" Then
_validatorExpression = "((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}( x\d{0,})?"
_errorMessage = "Accepts Phone Numbers Of The Format (XXX)XXX-XXXX xX Only."
'If its DOB... set it to check for a valid Date Of Birth entry
ElseIf _validExpression = "DOB" Then
_validatorExpression = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d"
_errorMessage = "Accepts Birth Date info (MM/DD/YYYY) Only."
'If an invalid entry was found in default.aspx... display the error and redirect.
Else
MsgBox("Well thats not good...")
End If
End If
End Set
End Property
Public WriteOnly Property SetErrorMessage() As String
Set(ByVal value As String)
If Len(Trim(value)) > 0 Then
' Retrieve the error message from the default.aspx page and set it
_errorMessage = Trim(value)
End If
End Set
End Property
Public WriteOnly Property SetLabelValue() As String
Set(ByVal value As String)
If Len(Trim(value)) > 0 Then
' Retrieve the value for the label from the default.aspx page and set it
_LabelMsg = Trim(value)
End If
End Set
End Property
Public WriteOnly Property SetTextOnorOff() As Boolean
Set(ByVal value As Boolean)
'Turn the TextBox on or off/ enabled or not enabled
_TextSwitch = value
End Set
End Property
Public WriteOnly Property SetLabelVisibility() As Boolean
Set(ByVal value As Boolean)
'Turns the Label On or Off
_LabelISeeU = value
End Set
End Property
#End Region
#End Region
#Region "Method & Events"
#Region "Public - Method & Events"
#End Region
#Region "Private - Method & Events"
' Random Image Control's Page Load Event
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblError.ErrorMessage = _errorMessage
lblError.ValidationExpression = _validatorExpression
' Load the Label
' Label will be enabled or disabled this way
txtValidText.Enabled = _TextSwitch
' Make the label visible or invisible
lblTextValid.Enabled = _LabelISeeU
'Now we have to make sure the label is working
If lblTextValid.Visible = True Then
' If it can be seen, display a message
lblTextValid.Text = _LabelMsg
Else
'Else, dont do anything at all
lblTextValid.Text = ""
End If
' Clean up the Validation Control Variables
'This will execute last
VarCleanUp()
End Sub
Public Sub Update()
'Updates our updatePanel on the ascx Page
UpdatePanel1.Update()
End Sub
' Initialize the variables for the validation Controller
Private Sub InitVars()
_LabelMsg = String.Empty
_TextSwitch = Nothing
_LabelISeeU = Nothing
_errorMessage = String.Empty
_validExpression = String.Empty
_validatorExpression = String.Empty
_LabelMsg = String.Empty
End Sub
' Clean up all the variables in the validation controller when we are done
Private Sub VarCleanUp()
_LabelMsg = Nothing
_TextSwitch = Nothing
_LabelISeeU = Nothing
_errorMessage = Nothing
_validExpression = Nothing
_validatorExpression = Nothing
_LabelMsg = Nothing
End Sub
#End Region
#End Region
End Class
Here is the .ascx file itself (NumberValidator1) <%@ Control Language="VB" AutoEventWireup="false" CodeFile="NumberValidator1.ascx.vb" Inherits="Control_NumberValidator1" %> <link href="../NumberValidator.css" rel="stylesheet" type="text/css" /> <!-- Label for errors goes here--> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:RegularExpressionValidator ID="lblError" runat="server" ControlToValidate = "txtValidText"> </asp:RegularExpressionValidator> <br /> <!-- Text Label Goes Here --> <asp:Label ID = "lblTextValid" CssClass ="label" runat = "server" /> <!-- Text Box goes here --> <asp:TextBox ID = "txtValidText" runat = "server" onselectedIndexChanged = "txtValidIndex_onselectedIndexChanged" ></asp:TextBox> </ContentTemplate> </asp:UpdatePanel>
'Here is my default.aspx.vb file
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Turn off all text Boxes accept those within the user controls ' Enable or Disable the Textbox Control at will Validator.SetTextOnorOff = True ' Set the Label to remain visible or invisible Validator.SetLabelVisibility = True ' Assign a text value to the label itself and name it anything Validator.SetLabelValue = "Text Only Validation" ' Assign the Validation Expression a value 'Acceptable values for this field are (text, number, email, zip, phone) Validator.SetValidationSummary = "text" 'Once a value has been entered it will change the validation expression 'and error message automatically. ' Enable or Disable the Textbox Control at will Validator1.SetTextOnorOff = True ' Set the Label to remain visible or invisible Validator1.SetLabelVisibility = True ' Assign a text value to the label itself and name it anything Validator1.SetLabelValue = "Number Only Validation" ' Assign the Validation Expression a value 'Acceptable values for this field are (text, number, email, zip, phone) Validator1.SetValidationSummary = "number" 'Once a value has been entered it will change the validation expression 'and error message automatically. End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 'Pass the value in the text box to the User Control for further analysis TextBox1.Text = Validator.TextBoxValue End Sub Protected Sub Validator_OnTabIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Validator1.Update() Validator1.TextBoxValue = Validator.TextBoxValue End Sub End Class
'And here is the default.aspxpage itself
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <%@ Register src="Control/NumberValidator1.ascx" tagname="NumberValidator1" tagprefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <link href="NumberValidator.css" rel="stylesheet" type="text/css" /> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div> <!-- Text Only Validator --> <uc1:NumberValidator1 ID="Validator" UpdateMode = "Conditional" OnTabIndexChanged="Validator_OnTabIndexChanged" runat="server" /> </div> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <div> <!-- Number Only Validator --> <uc1:NumberValidator1 ID = "Validator1" runat = "server" /> </div> </ContentTemplate> </asp:UpdatePanel> </form> </body> </html>
Mod Edit: Please use code tags when posting your code. Code tags are used like so =>
Thanks,
PsychoCoder

New Topic/Question
This topic is locked




MultiQuote


|