当文本存在时,在输入框内插入检查图标

时间:2017-07-11 19:25:54

标签: html css validation input icons

当文本在输入框内时,我试图插入绿色复选图标。我试图通过添加一个fa-fa图标来做到这一点,但它似乎没有工作。另外,如果电子邮件无效,我想在输入框中放置一个红色的x图标。如果有人有任何见解如何做到这一点,我们将非常感谢您的帮助!附上小提琴和图像。

Green Check and Rex x icons

JsFiddle

<form action="">
    <div class="container" id="container">
        <label>First Name
          <input id="first_name" maxlength="40" name="first_name" size="20" type="text"><i class="fa fa-check-circle" aria-hidden="true"></i>
        </label>
        <label>Last Name
          <input id="last_name" maxlength="80" name="last_name" size="20" type="text">
        </label>
        <label>Email
          <input id="email" maxlength="80" name="email" size="20" type="text"><i class="fa fa-times-circle-o" aria-hidden="true"></i>
<!--            <span class="msg error">Wrong email!</span>
            <span class="msg success">Correct email!</span> -->
        </label>
        <label>Phone
          <input id="phone" maxlength="40" name="phone" size="20" type="text">
        </label>
        <label>City
          <input id="city" name="city" maxlength="40" size="20" type="text">
        </label>
        <label>State/Province 
            <input id="state" maxlength="20" name="state" size="20" type="text">
        </label>
        <label id="company">Company
          <input id="company" name="company" type="text">
        </label>
        <label>Comments 
          <textarea name="" id="" cols="30" rows="10"></textarea>
         <button type="submit" name="submit">SUBMIT</button>
        </label>
    </div>
</form>

body {
  color: #fff;
  background-color: #f78e2a;
  text-align: center;
}

form {
  color: #fff;
  background-color: #f78e2a;
  text-align: center;
  font-family: Lato;
}

* {
  box-sizing: border-box;
}

h1 {
  font-size: 20px;
  text-align: center;
}


input[type="text"] {
    width: 100%;
    padding: 10px;
    background-color: #f9a558;
    border: 1px solid #fff;
}

input[type="text"]:focus {
    background-color: #fff;
}

input[type="text"]:visited {
    background-color: #fff;
}
.container {
  display: flex;
  flex-direction: column;
  padding: 5px 0;
  margin-left: 10%;
  margin-right: 10%;
}

textarea {
  width:100%;
  background-color: #f9a558;
  border: 1px solid #fff;
}

textarea:focus {
  background-color: #fff;
}

#company {
  flex-basis: 100%;
  max-width: 100%;
}

label:nth-last-child(-n+2)
{
  flex-basis: 100%;
  max-width: 100%;
}


select, label {
    height: 50px;
    width: 48%;
    margin: 2% 1%;
    text-align: left;
}


button {
  margin-top: 10px;
  background-color: #B9B9B9;;
  color: #959595;
  border-radius: 6px;
  width: 120px;
  height: 35px;
  margin-left: 1%;
  display: block;
}

.fa fa-check-circle {
  color: green;
}

button:focus{
  background-color: #fff;
  color: #f78e2a;
}




@media (max-width: 426px) {
  label {
    width: 98%;
  }

}

@media (min-width: 426px) {
  .container {
    flex-direction: row;
    flex-wrap: wrap;
    align-self: flex-start;
  }


}

2 个答案:

答案 0 :(得分:1)

使用javascript执行此操作的方法是检查keyup上的input,如果有值,则显示图标。我在下面使用jQuery。

对于您的电子邮件,您需要使用某种验证模式 - 如果电子邮件输入包含内容,默认情况下会显示一个红色图标,但如果它与验证模式匹配,请将图标更改为绿色。我从How to validate email address in JavaScript?

获得了电子邮件验证正则表达式

function validateEmail(email) {
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}

$('input[type="text"]').on('keyup', function() {
  if ($(this).val().trim() != '') {
    if ($(this).is('#email')) {
    	if (validateEmail($(this).val())) {
      	$(this).attr('data-valid','valid');
      } else {
      	$(this).attr('data-valid','error');
      }
    } else {
      $(this).attr('data-valid','valid');
    }
  } else {
    $(this).removeAttr('data-valid');
  }
});
body {
  color: #fff;
  background-color: #f78e2a;
  text-align: center;
}

form {
  color: #fff;
  background-color: #f78e2a;
  text-align: center;
  font-family: Lato;
}

* {
  box-sizing: border-box;
}

h1 {
  font-size: 20px;
  text-align: center;
}

input[type="text"] {
  width: 100%;
  padding: 10px;
  background-color: #f9a558;
  border: 1px solid #fff;
}

input[type="text"]:focus {
  background-color: #fff;
}

input[type="text"]:visited {
  background-color: #fff;
}

.container {
  display: flex;
  flex-direction: column;
  padding: 5px 0;
  margin-left: 10%;
  margin-right: 10%;
}

textarea {
  width: 100%;
  background-color: #f9a558;
  border: 1px solid #fff;
}

textarea:focus {
  background-color: #fff;
}

#company {
  flex-basis: 100%;
  max-width: 100%;
}

label:nth-last-child(-n+2) {
  flex-basis: 100%;
  max-width: 100%;
}

select,
label {
  height: 50px;
  width: 48%;
  margin: 2% 1%;
  text-align: left;
}

button {
  margin-top: 10px;
  background-color: #B9B9B9;
  ;
  color: #959595;
  border-radius: 6px;
  width: 120px;
  height: 35px;
  margin-left: 1%;
  display: block;
}

.fa fa-check-circle {
  color: green;
}

button:focus {
  background-color: #fff;
  color: #f78e2a;
}

@media (max-width: 426px) {
  label {
    width: 98%;
  }
}

@media (min-width: 426px) {
  .container {
    flex-direction: row;
    flex-wrap: wrap;
    align-self: flex-start;
  }
}

label {
  position: relative;
}

.fa {
  position: absolute;
  bottom: 0;
  right: 0;
  transform: translate(-50%, -45%);
  opacity: 0;
  transition: opacity .5s, color .5s;
}

[data-valid] + .fa {
  opacity: 1;
}

[data-valid="valid"] + .fa {
  color: green;
}

[data-valid="error"] + .fa {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
  <div class="container" id="container">
    <label>First Name
      <input id="first_name" maxlength="40" name="first_name" size="20" type="text"><i class="fa fa-check-circle" aria-hidden="true"></i>
    </label>
    <label>Last Name
      <input id="last_name" maxlength="80" name="last_name" size="20" type="text">
    </label>
    <label>Email
      <input id="email" maxlength="80" name="email" size="20" type="text"><i class="fa fa-times-circle-o" aria-hidden="true"></i>
      <!-- 			<span class="msg error">Wrong email!</span>
			<span class="msg success">Correct email!</span> -->
    </label>
    <label>Phone
      <input id="phone" maxlength="40" name="phone" size="20" type="text">
    </label>
    <label>City
      <input id="city" name="city" maxlength="40" size="20" type="text">
    </label>
    <label>State/Province
      <input id="state" maxlength="20" name="state" size="20" type="text">
    </label>
    <label id="company">Company
      <input id="company" name="company" type="text">
    </label>
    <label>Comments
      <textarea name="" id="" cols="30" rows="10"></textarea>
      <button type="submit" name="submit">SUBMIT</button>
    </label>
  </div>
</form>

答案 1 :(得分:0)

首先,input内有ilabel个标记。这需要修复。完成后,您应为每个字段创建一个父div。这样您就可以使用position: absolute;作为图标。

这样的事情:

<强> HTML

<div class="field">
    <label>First Name</label>
    <input id="first_name" maxlength="40" name="first_name" size="20" type="text" />
    <i class="fa fa-check-circle" aria-hidden="true"></i>    
</div>

<强> CSS

.field {
    position: relative;
    margin: 10px;
}

.field i.fa {
    position: absolute;
    top: 5px;
    right: 5px;
}