Search code examples
htmlcssasp.netrequiredfieldvalidator

asp textbox required field validator message positioning


I have used a asp:RequiredFieldValidator to validate a textbox. It works fine. But i need to change the position of the error message.

<div class="form-group">
                <label for="txtGarageName" class="col-sm-2 control-label">Garage Name</label>
                <asp:TextBox runat="server" placeholder="Garage Name" CssClass="form-control" Width="50%" ID="txtGarageName"></asp:TextBox>
                      <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
                      ControlToValidate="txtGarageName"
                      ErrorMessage="Garage name is a required field."
                      ForeColor="Red">
                  </asp:RequiredFieldValidator>
              </div>

When i use this error message display starting from directly under the label. What i need to do is get the error message to display either in front of the text box or align it with the TextBox start point which means to alighn it more to right. I have tried using a css code as well as putting

margin-left=""

which i saw as solutions in the internet. Neither worked properly.


Solution

  • Using the col-md-6 or span6 CSS class for your TextBoxes will work for you

    <asp:TextBox runat="server" placeholder="Garage Name" CssClass="form-control span6"  ID="txtGarageName"></asp:TextBox>
    
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
                              ControlToValidate="txtGarageName"
                              ErrorMessage="Garage name is a required field."
                              ForeColor="Red">
                          </asp:RequiredFieldValidator>