Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / Visual Basic

Standard CustomValidator Textbox

Rate me:
Please Sign up or sign in to vote.
4.77/5 (8 votes)
4 Jun 2008CPOL3 min read 40.9K   369   20  
The Custom Control eliminates the use of adding validators for each and every textbox added. Just by setting a couple of properties we can make our textbox validate credit card numbers, phone numbers or any other Custom expression. It provides some other built in functionalities.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Tata Consultancy Services
India India
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.

Comments and Discussions