根据验证结果切换元素可见性

时间:2016-07-04 05:08:07

标签: javascript jquery validation twitter-bootstrap-3 asp.net-core

给出以下HTML

<form class="form-horizontal"
      asp-controller="Installation"
      asp-action="CreateUser"
      method="post">
    <fieldset>

        <!-- Form Name -->
        <legend>Account Creation</legend>

        <!-- Username input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="userName">Username</label>
            <div class="col-md-4">
                <input id="UserName" name="UserName" asp-for="UserName" type="text" placeholder="Username" class="form-control input-md" required="">
                <span id="usernameTip" class="help-block hidden">Enter a unique Username</span>
                <span class="has-error" asp-validation-for="UserName"></span>
            </div>
        </div>

        <!-- Email input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="email">E-mail</label>
            <div class="col-md-4">
                <input id="email"
                       name="email"
                       type="text"
                       placeholder="jane@doe.com"
                       class="form-control input-md"
                       required=""
                       asp-for="Email">
                <span class="help-block">Enter your e-mail address</span>
            </div>
        </div>

        <!-- Password input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="password">Password</label>
            <div class="col-md-4">
                <input id="password"
                       name="password"
                       type="password"
                       placeholder="password"
                       class="form-control input-md"
                       required=""
                       asp-for="Password">
                <span class="help-block">Enter a password that is at least 8 characters, fewer than 30</span>
            </div>
        </div>

        <!-- Password input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="confirmPassword">Confirm Password</label>
            <div class="col-md-4">
                <input id="confirmPassword"
                       name="confirmPassword"
                       type="password"
                       placeholder="password"
                       class="form-control input-md"
                       required=""
                       asp-for="PasswordConfirmation">
                <span class="help-block">Re-enter your password</span>
            </div>
        </div>

        <!-- Submit -->
        <div class="form-group">
            <label class="col-md-4 control-label"
                   for="createAccount"></label>
            <div class="col-md-4">
                <button id="createAccount" type="submit" name="createAccount" class="btn btn-primary">Create Account</button>
            </div>
        </div>

    </fieldset>
</form>

我希望隐藏/显示usernameTip范围,当asp-validation-for范围包含验证错误时,以及当有相邻{时我用作提示的任何其他范围{ {1}} span。

阅读this post我可以获得显示/隐藏范围所需的JavaScript。我唯一无法弄清楚的是,如果View实际上知道庄园中的验证错误,那么如果错误存在,我可以有条件地隐藏/显示asp-validation-for跨度。

有没有人知道我需要做什么才能根据模型上的数据注释错误切换可见性?

在Chrome浏览器中,usernameTip span在编译时转为此内容:

asp-validation-for

修改

验证失败时,末尾会附加<span class="has-error field-validation-error" data-valmsg-for="UserName" data-valmsg-replace="true">Usernames must be between 2 and 50 characters</span>

用户名必须介于2到50个字符之间== $ 0

当验证失败时,我没有看到任何类在span中添加或删除。

编辑2

我使用接受的答案来解决这个问题。但是对于那些未来的人来说,您可以在视图中使用MVC的==$0属性来确定是否存在错误。

ViewData

1 个答案:

答案 0 :(得分:1)

<div class="col-md-4">
    <input id="UserName" name="UserName" asp-for="UserName" type="text" placeholder="Username" class="form-control input-md" required="">
    <span id="usernameTip" class="help-block hidden">Enter a unique Username</span>
    <span id="error" class="has-error" asp-validation-for="UserName"></span>
</div>
    <script>

   if( $("#error").text().length>0){
//show usernameTip 
}
</script>