DaniKR
DaniKR

Reputation: 2448

How to put Bootstrap ValidationMessageFor in correct position?

I am using bootstrap for my web application, and I have problem with validationmessagefor puting message state in correct position. Now I have use this example from this link, with this code:

<div class="control-group error">
  <label class="control-label" for="inputError">Input with error</label>
  <div class="controls">
    <input type="text" id="inputError">
    <span class="help-inline">Please correct the error</span>
  </div>
</div>

Now code that I use:

<h4 class="form-signin-heading">Please sign in</h4>
<div class="control-group">
    <div class="controls">                 
        @Html.TextBoxFor(u => u.Username, new {placeholder="Username", @class="form-control username", id="inputError"}) 
        <span class="help-inner">@Html.ValidationMessageFor(u => u.Username)</span>                        
    </div>

    <div class="controls">       
        @Html.PasswordFor(u => u.Password, new {placeholder="Password", @class="form-control password"}) 
        @Html.ValidationMessageFor(u=> u.Password)
    </div>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>

My code do this (also in fiddle: http://jsfiddle.net/4h5E5/1/):

enter image description here

But my disire is to put validate message in this position (like example in link):

enter image description here

Upvotes: 5

Views: 8414

Answers (3)

Elnaz
Elnaz

Reputation: 2890

Use style = "display:inline" for EditorFor and ValidationMessageFor both, as below:

<div class="form-group">
    @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2"})

    <div class="col-md-9">
        @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", style = "display:inline" } })
        @Html.ValidationMessageFor(model => model.UserName, "*", new { @class = "text-danger", style = "display:inline" })
    </div>

</div>

Upvotes: 1

DevangKantharia
DevangKantharia

Reputation: 694

Please check this JS Fiddle Link

HTML Code:

<!DOCTYPE html>
<body>
    <div class="container">
        <form class="form-signin form-horizontal" role="form">
             <h4 class="form-signin-heading">Please sign in</h4>

            <div class="form-group">
                <div class="col-xs-7 col-sm-7">
                    <input type="username" class="form-control username" placeholder="Username" required="autofocus" />
                </div>
                <div class="col-xs-7 col-sm-5"> <span class="help-inline pull-left">Please correct the error</span>

                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-7 col-sm-7">
                    <input type="password" class="form-control password" placeholder="Password" />
                </div>
                <div class="col-xs-7 col-sm-5"> <span class="help-inline pull-left">Please correct the error</span>

                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-7 col-sm-7">
                    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
                </div>
            </div>
        </form>
    </div>
    <!-- /container -->
    <!-- Bootstrap core JavaScript==================================================- ->
    <!-- Placed at the end of the document so the pages load faster -->
</body>

I hope this is what you want to achieve.

If you have any other issue or if not solved, then please add a comment below.

Regards D.

Upvotes: 5

Wilf
Wilf

Reputation: 2315

Have you tried this? (untested)

  <div class="controls">
    <input type="text" id="inputError" class="pull-left">
    <span class="help-inline pull-left">Please correct the error</span>
  </div>

ps.would be great if you make a fiddle.

Upvotes: 1

Related Questions