开发者

how to use RegularExpressionValidator on textbox

I have an text box i need to validate so that the user can enter enter up to four character and they can be 开发者_Go百科alphanumeric. I am using VS2003, .NET 1.1.

Please let me know what is the expression i should use to validate this condition any help would be great. Thanks!

Tried like this:

<asp:TextBox id="wtxtTPP" tabIndex="2" runat="server" CssClass="text" Width="144px" MaxLength="4" />
<asp:RegularExpressionValidator id="RegularExpressionValidator1" style="z-index: 101; left: 208px; position: absolute; TOP: 16px" runat="server" ErrorMessage="RegularExpressionValidator" ValidationExpression="^([\S\s]{0,4})$" ControlToValidate="wtxtTPP" />
<input style="z-index: 102; left: 88px; position: absolute; top: 72px" type="submit" value="Submit" id="Submit1" name="Submit1" runat="server">


As you said, use a Regular Expression Validator and set the expression to something like this:

^([\S\s]{0,4})$

Replace the 4 with your desired max length.

Update:

<asp:TextBox id="wtxtTPP" Runat="server" />

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" 
        ErrorMessage="RegularExpressionValidator" 
        ValidationExpression="^([\S\s]{0,4})$" 
        ControlToValidate="wtxtTPP" />

<asp:Button ID="Button1" runat="server" Text="Button" />

This works just fine for me. I replaced your submit button with a normal asp.net button and simplified out all the unneeded stuff for the example.
In general, if you only have a one line textbox, you can just limit the text length with MaxLength="4" as you did. No need for a Validator.


Like @Remy said.

Also, the {0,4} part of the Regexp means length should be zero to a max of four so will allow for zero-length, i.e. no input. Remember to use a RequiredFieldValidator if the number is mandatory or replace the zero with a minimum number of digits.


       <table>
         <tr>   
           <td>E-mail Address:</td>
           <td>
              <asp:TextBox ID="txt_address" runat="server"></asp:TextBox>
           </td>
           <td>
             <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat ="server" 
              ErrorMessage="Please give Email Address! "
              ControlToValidate="txt_address"
              ValidationExpression="\S+@\S+\.\S+\w+"//example (example@yahoo.com)
              ForeColor="Red" >
             </asp:RegularExpressionValidator>
           </td>
         </tr>
       </table>

    <table>
      <tr>            
        <td>
           <asp:RadioButtonList ID="rbl_gender" runat="server">
            <asp:ListItem>Male</asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
            </asp:RadioButtonList>
         </td>
         <td>
            <asp:RequiredFieldValidator ID="RequiredFieldVlidator2" runat="server"
            ErrorMessage="Please Choose your Gender" ControlToValidate="rbl_gender"
            ForeColor="Red" >
            </asp:RequiredFieldValidator>
         </td>
       </tr>
   </table>

<table>
   <tr>    
     <td>
         <asp:RequiredFieldValidator ID="RequiredFeildValidator1" 
            runat ="server" ControlToValidate ="txt_name" 
            ErrorMessage="Please Insert Your name !" 
            ForeColor="Red">
         </asp:RequiredFieldValidator>
     </td>
   </tr>    


A very small change in the ValidationExpression seems to make the difference between only sometimes working, and reliably working. The following worked for me in development, but not in production:

<asp:RegularExpressionValidator Display = "Dynamic" ControlToValidate = "tbCarePlan" ID="revCarePlan" ValidationExpression = "^(.{0,4000})$" runat="server" ErrorMessage="Max 4000 characters allowed." />

Replacing the ValidationExpression above with "[\s\S]{0,4000}" it now works reliably. Leave out the ^ and $ - the RegularExpressionValidator does not require this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜