简体   繁体   中英

how to display text in a table dynamically?

im using asp.net..In login form im using requiredfield validator and regularexpression validator for username.so i need to display the error message in same td for both validation.that first it need to validate required field and display the error msg in td.if that is field it need to checkfor that expression and overite the error message in same td..

This my html.on button click validations are working but error message are displaying in consecuent position..

       <tr>
        <td>
            <asp:Label ID="l_uname" runat="server" CssClass="label" Text="User Name" ></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="t_uname" runat="server" CssClass="text" ToolTip="Enter Username"></asp:TextBox>
        </td>           
        <td>
        <label class="l" runat="server">*</label>
        </td>
        <td>
        <asp:RequiredFieldValidator ID="rf_uname" CssClass="t" runat="server"           ErrorMessage="Enter the username" 
          ControlToValidate="t_uname"    ValidationGroup="logingroup"></asp:RequiredFieldValidator>
         <asp:RegularExpressionValidator ID="Re_name" runat="server"
            CssClass="t"  ControlToValidate="t_name" ValidationGroup="logingroup" 
            ValidationExpression="^[A-Za-z ]{6,20}$" ErrorMessage="Name should be 6-20 character">
            </asp:RegularExpressionValidator>
        </td>
    </tr>
    <asp:Button ID="login" runat="server" Text="Login" ValidationGroup="logingroup"     onclick="login_Click"/>

add display dynamic (Display="Dynamic") to both the validator control they won't be displaying in consecuent position..

Like this

 <asp:RequiredFieldValidator ID="rf_uname" CssClass="t" runat="server"           ErrorMessage="Enter the username" 
              ControlToValidate="t_uname" Display="Dynamic" ValidationGroup="logingroup"></asp:RequiredFieldValidator>
             <asp:RegularExpressionValidator ID="Re_name" Display="Dynamic" runat="server"
                CssClass="t"  ControlToValidate="t_name" ValidationGroup="logingroup" 
                ValidationExpression="^[A-Za-z ]{6,20}$" ErrorMessage="Name should be 6-20 character">
                </asp:RegularExpressionValidator>

Hope this helps

if you have a td with css class "tdclass" then use text() method

$('.tdclass').text("message");

if you want to write Html then use html() method

Both validators will run together. To run them out at a time, you will need to Enable the second validator at server side. Not a very good approach.

A better approach would be to have only one Regex validator with a proper message to indicate that it is required and between 6-20 characters

 <asp:RegularExpressionValidator ID="Re_name" runat="server"
        CssClass="t"  ControlToValidate="t_name" ValidationGroup="logingroup" 
        ValidationExpression="^[A-Za-z ]{6,20}$" 
        ErrorMessage="Username is required to be between 6-20 characters">
        </asp:RegularExpressionValidator>

For clientside validation, take a look at the jQuery Validation plugin. It is simple, all you need to do is add css classes to tell it what validation to apply. It does support more complex rules where needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM