Server Validation for six digit numbers:"\d{6}". How to avoid empty input? Thanks.

Posted on 2004-11-08
Last Modified: 2012-05-05
I have a server validation for ID number validation in the text box field, the regular expression is:
It works well when the user input is not exactly six digit number except it can't detext zero, i.e. when user doesn't input any thing and press enter, it will pass to the next step processing. How to change the regular expression so that it can exclude empty input?

Thanks a lot,
Question by:heyday2004
    LVL 20

    Assisted Solution


    First suggestion,
    Why not just add a RequireFieldValidator to handle empty input.
    LVL 6

    Assisted Solution

    have you thought about using a  RequiredFieldValidator ?
    LVL 20

    Assisted Solution

    One advantage using RequireFieldValidator is that you can detect that the input is empty then you can give better error message.
    LVL 4

    Expert Comment

    Hi, I give it a test on with your express and don't input anything and it didn't pass.
    Can you post the code ?
    Nice day

    Author Comment

    Thanks a lot for the prompt responses. I wanted to know if there is any way to use only one regular expression validation here and also curious why the regular expression "\d{6}" doesn't work for empty input. Below is my code, thanks again for all the answers:

    <asp:button id="btnEnter" style="Z-INDEX: 101; LEFT: 656px; POSITION: absolute; TOP: 240px" runat="server" Height="36px" Font-Size="Small" Font-Bold="True" Text="ENTER"></asp:button>

    <asp:regularexpressionvalidator id="Regularexpressionvalidator2" style="Z-INDEX: 103; LEFT: 328px; POSITION: absolute; TOP: 288px" Height="24px" Runat="Server" Width="345px" Font-Size="X-Small" Text="Invalid ID number." ControlToValidate="TextBox1"  Display="Dynamic" ValidationExpression="\d{6}">Error! Shoud enter a 6 digit badge number here. <asp:regularexpressionvalidator>

    LVL 4

    Assisted Solution

    Hi Heyday2004,
    I think that you can't do the regular expression validate and also check null in <asp:regularexpressionvalidator control.
    You should create a requiredfieldvalidator for your textbox, or you can use the custom.
    <asp:CustomValidator id="valCustom" runat="server"
        ErrorMessage="*This box is not valid" dispaly="dynamic">*

    'and validate what you want in ServerValidate
    Sub ServerValidate (objSource As Object, objArgs As ServerValidateEventsArgs)
        ' Code goes here
    End Sub
    Nice day.
    LVL 28

    Assisted Solution

    Hi heyday2004,

    here is the remarks from MSDN :
    Note   Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.

    LVL 6

    Accepted Solution

    Hi heyday,
     What mmarinov said is 100% correct. You can not validate against a empty control using RegularExpressionValidator or CustomValidator or RangeValidator or any other type validator except RequiredFieldValidator. Usually when you need to use any of these mentioned validators, they should be coupled with RequiredFieldValidator, which kicks in prior to your RegularExpressionValidator, and throws an error. This is the common mistake we all do.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now