How do I validate the value of a textbox from 0000 thru 9999 on the server side via VB.NET?

Posted on 2008-11-04
Last Modified: 2010-08-05
I need to validate if a textbox's value is between 0000 thru 9999 (with padded zeroes).  I have to implement it on the server side using VB.NET code.

How can I do this?
Question by:crickpaolo
    LVL 44

    Accepted Solution

    Hello crickpaolo,

    A regular expression of \d{4} will validate that easily, you can implement this as a regular expression validator client-side and then again server-side.


      Imports System.Text.RegularExpressions 
    Public Dim regex As Regex = New Regex( _
          "\d{4}", _
        RegexOptions.IgnoreCase _
        Or RegexOptions.CultureInvariant _
        Or RegexOptions.IgnorePatternWhitespace _
        Or RegexOptions.Compiled _
    Dim IsMatch As Boolean = regex.IsMatch(InputText)

    Open in new window


    Author Closing Comment

    thanks man.
    LVL 26

    Expert Comment

    by:Anurag Thakur
    make sure that your text box takes maximum input of 4 characters that also only numbers and no special characters alphabets and space or a dot
    i will suggest you to do the first level of test by using a regularexpression validator then only verify again at the server side - why do you want to do a round trip for this kind of verification which can be easily done on the client side by regularexpression validator and the expression will be whats defined by TimCottee in the previous comment

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now