|
Imports System.Web.UI.WebControls
Namespace CustomCtrl
Public Class CustTextBox
Inherits TextBox
Private _ValidateItem As ValidateValue
Private _IsRequired As Boolean
Private _ExpressionValue As String
'Oninit we will set the
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
Dim strRegExp As String = ""
Select Case ValidateItem
Case ValidateValue.Email
strRegExp = "^([\\w\\-\\.]+)@((\\[([0-9]{1,3}\\.){3}[0-9]{1,3}\\])|(([\\w\\-]+\\.)+)([a-zA-Z]{2,4}))$"
Case ValidateValue.URL
strRegExp = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?"
Case ValidateValue.PhoneNumber
strRegExp = "((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}"
Case ValidateValue.CreditCard
strRegExp = "^(\\d{4}[- ]){3}\\d{4}|\\d{16}$"
Case ValidateValue.MonthYear
strRegExp = "(((0[123456789]|10|11|12)([/])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))"
Case ValidateValue.MonthDayYear
strRegExp = "^(([1-9])|(0[1-9])|(1[0-2]))\\/(([0-9])|([0-2][0-9])|(3[0-1]))\\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$"
Case ValidateValue.Time
strRegExp = "^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$"
Case ValidateValue.CustomExpression
strRegExp = ExpressionValue
End Select
Me.Attributes.Add("onblur", "Check('" + strRegExp + "',this,'" + IsRequired.ToString + "')")
MyBase.OnInit(e)
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim strScript As String = "<script> function Check(regex,obj,reqd) {re = new RegExp(regex); var Prefix=obj.id; var Ctrl=document.getElementById(Prefix);var value=Ctrl.value; if (reqd=='True'){if (value==''){alert('Value Required');Ctrl.focus();}else{if (re.test(value)) {alert('Valid');} else { alert('InValid'); Ctrl.focus(); }} }else {if (value==''){}else{if (re.test(value)) {alert('Valid');} else { alert('InValid'); Ctrl.focus();}}}}</script> "
writer.WriteLine(strScript)
MyBase.Render(writer)
End Sub
Public Enum ValidateValue
Email
PhoneNumber
URL
CreditCard
MonthYear
MonthDayYear
Time
CustomExpression
End Enum
Public Property ValidateItem() As ValidateValue
Get
Return _ValidateItem
End Get
Set(ByVal value As ValidateValue)
_ValidateItem = value
End Set
End Property
Public Property IsRequired() As Boolean
Get
Return _IsRequired
End Get
Set(ByVal value As Boolean)
_IsRequired = value
End Set
End Property
Public Property ExpressionValue() As String
Get
Return _ExpressionValue
End Get
Set(ByVal value As String)
_ExpressionValue = value
End Set
End Property
End Class
End Namespace
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have been into Web development for past 2+ years.I've worked on areas like ASP.NET,VB.Net,Web services and web application framework DotNetNuke.I'm Microsoft Certified Professional in ASP.Net.