css中的表单验证无法正常工作

时间:2017-07-13 06:35:21

标签: jquery html css twitter-bootstrap

我创建了一个注册html页面,我试图在表单验证的基础上为文本字段添加效果。

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/bootstrap.css">

        <!-- Optional theme -->
        <link rel="stylesheet" href="css/bootstrap-theme.css">

        <script src="js/jquery.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">
        <!-- Latest compiled and minified JavaScript -->
        <script src="js/bootstrap.js"></script>
        <!-- <script src="js/FormValidation.js"></script> -->
        <script src="https://use.fontawesome.com/b1af24592c.js"></script>
    </head>
    <body>
        <div>
                <div class="col-md-3">
                </div>
                <div class="col-md-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 style="text-align:center"><a style="text-decoration:none; color:#000000;" href="index.html">Form</a></h3>
                        </div>
                        <div class="panel-body">
                            <form>
                                <div class="form-group has-feedback has-warning">
                                    <label>First Name</label>
                                    <input type="text" placeholder="Your first name" class="form-control" id="firstName" required>
                                    <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
                                </div>
                                <div class="form-group has-feedback has-success">
                                    <label>Last Name</label>
                                    <input type="text" placeholder="Your last name" class="form-control" id="lastName" required>
                                    <span class="glyphicon glyphicon-ok form-control-feedback"></span>
                                </div>
                                <div class="form-group">
                                    <label>Email</label>
                                    <input type="text" placeholder="Your email id" class="form-control" id="email" required>
                                </div>
                                <div class="form-group">
                                    <label>Password</label>
                                    <input type="password" placeholder="Password" class="form-control" id="password" required>
                                </div>
                                <div class="form-group">
                                    <label>Confirm Password</label>
                                    <input type="password" placeholder="Confirm your password" class="form-control" id="confirmPassword" required>[![enter image description here][1]][1]
                                </div>
                            </form>
                        </div>
                        <div class="panel-footer">
                            <button class="btn btn-info btn-block">Register</button>
                            <h6>Already a user? <a href="#" data-toggle="modal" data-target="#modal-signin">Sign In</a></h6>
                        </div>
                    </div>
                </div>
        </div>
    </body>
</html>

在页面中,我想在文本框中添加glyphicon-ok和glyphicon-warning。

像这样的东西

enter image description here

但我得到的是:

enter image description here

可以采取哪些措施来解决此问题?

2 个答案:

答案 0 :(得分:0)

您必须将课程放在label上,并将inputicon span放入div,如下所示:

<div class="form-group has-feedback">
        <label for="fname" class="control-label col-md-6">First Name:</label>
        <div class="col-md-6">
            <input type="text" name="fname" id="fname" class="form-control" placeholder="Enter first name" />
            <span class="glyphicon form-control-feedback" id="fname1"></span>
       </div>
    </div>

FIDDLE

希望它能解决你的问题。

答案 1 :(得分:0)

您不能将:before:after与输入元素一起使用。因此,我在它周围包裹了一个span。这意味着输入验证需要影响相关的跨度以显示图标。

label {
  display: block;
  font-weight: bold;
}

.error {
  position: relative;
}

.error:after {
  font-family: FontAwesome;
  content: "\f071";
  position: absolute;
  top: .1em;
  right: .2em;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<label for="name">Name</label>
<span class="error"><input name="name" type="text" placeholder="name"></span>