5

I am using Bootstrap 3 with ASP.NET webforms and I am completely new to Bootstrap. The webform I am working on uses ASP.net validation controls. My web form has a standard layout with two columns and I am using "form-group" class to group my labels and input fields.

Now the problem is that as I am placing the label, input field as well as its validator in the Bootstrap "form-group" class, the message in the text property of the validator is being displayed in the next line after validation. I want that to be displayed right next to the input field. Is there any alternative way I can do this?

<div class="form-group">
    <asp:Label runat="server" AssociatedControlID="txtBox">Address <span class="required">*</span></asp:Label>
    <asp:TextBox id="txtBox" runat="server"  CssClass="form-control"/>
    <asp:RequiredFieldValidator ID="rfvLine1" ValidationGroup="<%# ValidationGroup %>" ControlToValidate="txtBox" runat="server" Display="Dynamic" ErrorMessage="Line 1 is required." Text="*"/>   
</div>
1
  • Based on the above code, Label and TextBox are stacked. How do you display in two columns (inline)? Commented Sep 7, 2016 at 15:56

3 Answers 3

2

Easiest way is to create validation in separate column.

enter image description here

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <style type="text/css">
        .required {
            color: #e31937;
            font-family: Verdana;
            margin: 0 5px;
        }

        .field-validation-error {
            color: #e31937;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <br />
        <div class="container form-horizontal">
            <div class="form-group">
                <asp:Label runat="server"
                    AssociatedControlID="txtBox"
                    CssClass="col-xs-12 col-sm-4 control-label">
                    Address 
                    <span class="required">*</span>
                </asp:Label>
                <div class="col-xs-12 col-sm-4 ">
                    <asp:TextBox ID="txtBox" runat="server"
                        CssClass="form-control" />
                </div>
                <div class="col-xs-12 col-sm-4 form-control-static">
                    <asp:RequiredFieldValidator ID="rfvLine1"
                        ControlToValidate="txtBox" runat="server"
                        Display="Dynamic"
                        CssClass="field-validation-error"
                        Text="Line 1 is required." />
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-12 col-sm-offset-4 col-sm-4 ">
                    <asp:Button runat="server" ID="SubmitButton"
                        Text="Submit" CssClass="btn btn-primary" />
                </div>
            </div>
        </div>
    </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Ah! I was trying to make it. Vote up.
Thanks Win! Its working. Although I did not use the same code, I incorporated your idea to perform validation in separate column and it worked. By the way the snippet I mentioned was just one set of form controls placed in one of the columns. I have divided the entire page into two columns each containing multiple set of form controls.
0

Do the following: Updated

<div class="container">
    <h4 class="form-signin-heading">Info</h4>
    <div class="form-group">
        <div class="col-xs-7 col-sm-7">
            <asp:TextBox runat="server" placeholder="Garage Name" CssClass="form-control username" ID="txtAddress"></asp:TextBox>
        </div>
        <div class="col-xs-7 col-sm-5">
            <span class="help-inline pull-left">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                    ControlToValidate="txtAddress"
                    ErrorMessage="Address is a required field."
                    ForeColor="Red" Display="Dynamic">
                </asp:RequiredFieldValidator>
            </span>
        </div>
    </div>
    <br />
    <br />

    <div class="form-group">
        <div class="col-xs-7 col-sm-7">
            <asp:Button ID="Button1" runat="server" Text="Add" CssClass="btn btn-lg btn-primary btn-block" />
        </div>
    </div>
</div>

I hope, this is what you expected:

Sample Image

By the way, you didn't put the end tag in the RequiredFieldValidator.

3 Comments

OP explicitly said Boostrap. OP wants to use Bootstrap's form-control class in TextBox.
OK. Got it. Thanks for notifying.
Updated the post.
0

#jQuery for check "require" validation for only submit button#

<script type="text/javascript">
 (function () {'use strict';
   window.addEventListener('load', function () {
    var form = document.getElementById('needs-validation');
    form.addEventListener('submit', function (event) {
     if (form.checkValidity() === false) 
       {
          event.preventDefault();
          event.stopPropagation();
       }
     form.classList.add('was-validated');
        }, false);
        }, false);
      })();

##Submit button HTML##

 <asp:Button ID="Button1"  runat="server" Text="Submit" class="btn btn-primary"   OnClick="Button1_Click" />

##Non Submit button##

 <asp:Button ID="btnSearch"  runat="server"    class="form-control btn-primary " Width="25%" Text="Find" UseSubmitBehavior="false" OnClick="btnSearch_Click" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.