使用自定义样式进行jQuery unobstrusive验证.NET MVC 5

时间:2017-07-09 10:52:41

标签: javascript jquery html css asp.net-mvc

我有这样的HTML标记:

  <div class="col-sm-6">
        <div class="main-contact">
            <form id="registerForm">
                @Html.TextBoxFor(model=>model.FirstName, new { @placeholder="First Name" })
                @Html.ValidationMessageFor(model=>model.FirstName)
                @Html.TextBoxFor(model => model.LastName, new { @placeholder = "Last Name" })
                @Html.ValidationMessageFor(model => model.LastName)
                @Html.TextBoxFor(model => model.Email, new { @placeholder = "Email" })
                @Html.ValidationMessageFor(model => model.Email)
                @Html.TextBoxFor(model => model.Password, new { @placeholder = "Password" })
                @Html.ValidationMessageFor(model => model.Password)
                @Html.TextBoxFor(model => model.RepeatPassword, new { @placeholder = "Repeat Password" })
                @Html.ValidationMessageFor(model => model.RepeatPassword)
                @Html.DropDownListFor(model => model.selectedCountryId, Model.Countries, "-- Select a country --")
                @Html.ValidationMessageFor(model => model.selectedCountryId)

                <button type="submit">Send message</button>
            </form>
        </div>
    </div>

使用这些库激活jQuery的不引人注意的验证:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>

现在,当表单无效时,我试图在文本框本身和验证消息中添加一些样式...?

我想使用bootstrap的默认方式显示消息并在文本框周围添加红色发光...或者如果你们中的一些人有一些想要在这里分享的自定义css ?

如何立即将默认引导样式应用于此或添加一些自定义样式?

1 个答案:

答案 0 :(得分:0)

添加对下面CDN的引用并修改您的表单以使用引导类来更改UI外观。

!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

这样的事,

 <div class="form-group">
        @Html.LabelFor(m => m.Name)
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control"})
        @Html.ValidationMessageFor(m => m.Name)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Description)
        @Html.TextAreaFor(m => m.Description, new { @class = "form-control", placeholder = "eg: details about the deal..." })
        @Html.ValidationMessageFor(m => m.Description)
    </div>
<button type="submit" class="btn btn-primary btn-lg">Submit</button>

此处表单组,表单控件类来自Bootstrap

此外,您可以参考此link以查看您需要为不同的表单元素使用哪些类