locked
How to write reular validation expression for my case? RRS feed

  • Question

  • User-609535877 posted

    I tried to use a ASP.Net Regular Expression Validator to validate user data entry to allow 6 digit numeric number only and no white space allowed with expression "^\d{6}$" but it doesn't work for white space validation. How to write correct expression for my case?

    Thanks in advance!

     

    Tuesday, December 5, 2017 7:48 PM

All replies

  • User2103319870 posted

    allow 6 digit numeric number only and no white space allowed with expression "^\d{6}$" but it doesn't work for white space validation.

    Your Regex properly validates six digits with out white space. However RegularExpression validator didnt throw any error if only white space is entered in textbox. This is by design

    From MSDN: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx

    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.

    So if you want to display an error message when textbox is empty you need to use RequiredFieldValidator as well

    <asp:TextBox ID="TextBox10" runat="server"></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TextBox10" ValidationExpression="^\d{6}$" runat="server" Display="Dynamic"  ErrorMessage="Enter only 6 digits"></asp:RegularExpressionValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox10" Display="Dynamic"  ErrorMessage="Please enter numbers in textbox"></asp:RequiredFieldValidator>
    
    Tuesday, December 5, 2017 8:18 PM
  • User-609535877 posted

    Thank you, a2h. My case is a user can optionally enter a 6-digit numeric number only or don't allow to enter any characters including white spaces.

    Tuesday, December 5, 2017 8:28 PM
  • User2103319870 posted

    My case is a user can optionally enter a 6-digit numeric number only or don't allow to enter any characters including white spaces.

    You can use javascript to implement a custom validation which allows only numbers in  textbox. Also You can use Maxlength property of textbox to make sure user wont enter more than 6 digits

    Sample Code

            <asp:TextBox ID="TextBox10" runat="server" onkeypress="return isValidNumber(event)" MaxLength="6"></asp:TextBox>
    
    
            <script type='text/javascript'>
                function isValidNumber(evt) {
                    evt = (evt) ? evt : window.event;
                    var charCode = (evt.which) ? evt.which : evt.keyCode;
                    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                        return false;
                    }
                    return true;
                }
            </script>
    Wednesday, December 6, 2017 1:08 AM
  • User-1838255255 posted

    Hi zhao790,

    According to your description, you want to only 6 digit numeric number and don't allow space occur. I make a sample, it works fine in my side, please check:

    Sample Code: 

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:RegularExpressionValidator ID="REV1" runat="server" ErrorMessage="Fail" ValidationExpression="^[0-9]{6}$" ControlToValidate="TextBox1"></asp:RegularExpressionValidator>
            </div>
        </form>
    </body>

    Result:

    Best Regards,

    Eric Du 

    Wednesday, December 6, 2017 3:20 AM
  • User-609535877 posted

    Thanks, Eric. I want to validate if a user just enter white spaces instead of number combined with white spaces it will fail.

    Wednesday, December 6, 2017 1:03 PM
  • User-1838255255 posted

    Hi zhao790,

    According to your update, if you want to validate white space, i think you could use RequiredFieldValidator to validate null input. 

    Best Regards,

    Eric Du 

    Thursday, December 7, 2017 9:10 AM