dongzong1866 2018-06-08 21:46
浏览 65

如何从注册表单中的表单字段中分离错误消息?

I'm working on a registration form for a website I'm building, but can't separate the echoed error messages from the text-boxes within the form. Every time I run it in my browser to test the error messages, the text-boxes are moved to the left (out of alignment) and the error messages appear to the right of them. Clearly it's aligned both of the objects in the center, but I only want the form fields to be centered. Any help you can offer is appreciated.

Here's my html and css code...

<!DOCTYPE html>
<html>
<head>
  <title>Register</title>
  <style>
  body {
    background-color: silver;
  }
  .welcome {
    text-align: center;
    font-family: "Arial";
    font-size: 30px;
    text-shadow: 2px 2px lightgrey;
    color: white;
    padding-top: 15px;
  }
  .form-group {
    text-align: center;
  }
  input[name=username], input[name=first_name], input[name=last_name], input[name=email],
  input[name=password], input[name=confirm_password] {
    border: 3px solid lightgrey;
    padding: 10px;
    background: white;
    margin: 0 0 10px 0;
    width: 250px;
    outline:0 !important;
  }
  .help-block {
    font-family: "Arial";
    font-size: 12pt;
    color: white;
  }
  input[type=submit] {
    color: grey;
    border: 3px solid lightgrey;
    padding: 5px;
    width: 100px;
    text-align: center;
  }
  input[type=submit]:focus {
    outline:0 !important;
  }
  input[type=submit]:hover {
    background-color: #ddd;
  }
  input[type=submit]:active {
    background-color: #aaa;
    color: lightgrey;
  }
  </style>
</head>
<div class="welcome">
  <h1>Register</h1>
</div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
    <input type="text" name="username" class="form-control" value="<?php echo $username; ?>" placeholder="Username">
    <span class="help-block"><?php echo $username_err; ?></span>
  </div>
  <div class="form-group <?php echo (!empty($first_name_err)) ? 'has-error' : ''; ?>">
    <input type="text" name="first_name" class="form-control" value="<?php echo $first_name; ?>" placeholder="First Name">
    <span class="help-block"><?php echo $first_name_err; ?></span>
  </div>
  <div class="form-group <?php echo (!empty($last_name_err)) ? 'has-error' : ''; ?>">
    <input type="text" name="last_name" class="form-control" value="<?php echo $last_name; ?>" placeholder="Last Name">
    <span class="help-block"><?php echo $last_name_err; ?></span>
  </div>
  <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
    <input type="text" name="email" class="form-control" value="<?php echo $email; ?>" placeholder="Email">
    <span class="help-block"><?php echo $email_err; ?></span>
  </div>
  <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
    <input type="password" name="password" class="form-control" value="<?php echo $password; ?>" placeholder="Password">
    <span class="help-block"><?php echo $password_err; ?></span>
  </div>
  <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
    <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>" placeholder="Confirm Password">
    <span class="help-block"><?php echo $confirm_password_err; ?></span>
  </div>
  <div class="form-group">
    <input type="submit" class="btn btn-primary" value="Submit">
  </div>
</form>
</html>

</div>
  • 写回答

1条回答 默认 最新

  • doutan3371 2018-06-08 21:55
    关注

    One way would be to make the form-group's display:inline-block and position:relative. After that, you have to center the form-group's with text-align:center on the form.

    Now, you could simply position the error messages absolute and move them top:0; and left: 270px (250px width of the inputs and 2*10px padding)


    If you just want the error message under the input box, a simple diplay:block; text-align:center; on the help-block would be enough

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100