locked
Use RegularExpressionValidator o force user to type specific word in TextBox RRS feed

  • Question

  • User-1559458579 posted

    I am using a I am using a RegularExpressionValidator and trying to make a user enter the word example in a TextBox in an ASPX page. I have seen many examples but none address what I am attempting to do and that is limit the characters to example as I am typing it here. And do it in the order as I have it type. I tried the following code but I know it's wrong. I can type the letter e and I'm allowed to submit the form.

                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*Typing approved is required" ControlToValidate="TextBox7" ForeColor="Red" ValidationExpression="[e|x|a|m|p|l|e]"></asp:RegularExpressionValidator>
    



    Wednesday, October 7, 2020 9:02 PM

Answers

  • User-1590999572 posted

    <asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" errormessage="*Typing approved is required" controltovalidate="TextBox7" forecolor="Red" validationexpression="example"></asp:regularexpressionvalidator>

    Hi

    If I understood your query correctly, you want to force an exact word to be written in a TextBox, for example to write "example". If so, all you have to do is configure the RegularExpressionValidator Control like this:

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*Typing approved is required" ControlToValidate="TextBox7" ForeColor="Red" ValidationExpression="example"></asp:RegularExpressionValidator>

    This forces exact text to be inserted. Case sensitive.

    I hope it is useful for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 5:18 AM
  • User-1330468790 posted

    Hi dmartinm,

     

    You could validate the textbox input with a specific word using RegularExpressionValidator. However, it lacks of the property for case sensitive/insensitive.

     

    If you want to get the target word no matter it is in upper case or lower case, you should define a custom validator.

    More details, you could refer to below codes:

     <form id="form1" runat="server">
            <h3>Validation for a specific word</h3>
            <div>
                <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
                <div>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                     ErrorMessage="*Typing approved is required" 
                     ControlToValidate="TextBox7" ForeColor="Red" 
                     ValidationExpression="^example$" >
                 </asp:RegularExpressionValidator>
                </div>
                <div>
                    <asp:CustomValidator ID="customerValidator" runat="server" 
                    ControlToValidate="TextBox7" 
                    ErrorMessage="Must be the word 'example', case insensitive" 
                    ForeColor="Red" EnableClientScript="true" 
                    ClientValidationFunction="ValidateTextbox7"
                    ValidateEmptyText="true"></asp:CustomValidator>
                </div>
                 
                
                
            </div>
            <script type="text/javascript">
                function ValidateTextbox7(sender, args) {
                    var regex = /^example$/i;
                    console.log(args);
                    args.IsValid = regex.test(args.Value);
                }
            </script>
        </form>

    Demo:

      

    Hope helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 7:35 AM

All replies

  • User-1559458579 posted

    Well, I think this is something that just can't be done. It would have been nice but I will try and pre-populate the filed with the value I want.

    Wednesday, October 7, 2020 10:57 PM
  • User-1590999572 posted

    <asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" errormessage="*Typing approved is required" controltovalidate="TextBox7" forecolor="Red" validationexpression="example"></asp:regularexpressionvalidator>

    Hi

    If I understood your query correctly, you want to force an exact word to be written in a TextBox, for example to write "example". If so, all you have to do is configure the RegularExpressionValidator Control like this:

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*Typing approved is required" ControlToValidate="TextBox7" ForeColor="Red" ValidationExpression="example"></asp:RegularExpressionValidator>

    This forces exact text to be inserted. Case sensitive.

    I hope it is useful for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 5:18 AM
  • User-1330468790 posted

    Hi dmartinm,

     

    You could validate the textbox input with a specific word using RegularExpressionValidator. However, it lacks of the property for case sensitive/insensitive.

     

    If you want to get the target word no matter it is in upper case or lower case, you should define a custom validator.

    More details, you could refer to below codes:

     <form id="form1" runat="server">
            <h3>Validation for a specific word</h3>
            <div>
                <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
                <div>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                     ErrorMessage="*Typing approved is required" 
                     ControlToValidate="TextBox7" ForeColor="Red" 
                     ValidationExpression="^example$" >
                 </asp:RegularExpressionValidator>
                </div>
                <div>
                    <asp:CustomValidator ID="customerValidator" runat="server" 
                    ControlToValidate="TextBox7" 
                    ErrorMessage="Must be the word 'example', case insensitive" 
                    ForeColor="Red" EnableClientScript="true" 
                    ClientValidationFunction="ValidateTextbox7"
                    ValidateEmptyText="true"></asp:CustomValidator>
                </div>
                 
                
                
            </div>
            <script type="text/javascript">
                function ValidateTextbox7(sender, args) {
                    var regex = /^example$/i;
                    console.log(args);
                    args.IsValid = regex.test(args.Value);
                }
            </script>
        </form>

    Demo:

      

    Hope helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 7:35 AM
  • User-1559458579 posted

    Awesome solution! Thank you @Skar.2007

    Thursday, October 8, 2020 11:12 AM
  • User-1559458579 posted

    Thank you @Sean Fang. The Case logic will certainly be useful!

    Thursday, October 8, 2020 11:13 AM