简体   繁体   English

html css有效性在maxlength上失败

[英]html css validity fails on maxlength

I was playing with this https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text#Validation Where they validate that a required input value before the form is submitted when page initially loads.我正在玩这个https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text#Validation在页面最初加载时,他们在提交表单之前验证所需的输入值。

How can I do the EXACT same thing but with a maxlength on an input value has initial data that should fail validity?我怎样才能做完全相同的事情,但输入值的maxlength具有应该失败的初始数据?

Here is a jsFiddle I created to see this这是我创建的一个jsFiddle 来查看这个

. . CSS CSS

div {
  margin-bottom: 10px;
  position: relative;
}

input + span {
  padding-right: 130px;
}

input:invalid+span:after {
  position: absolute; content: '✖';
  padding-left: 5px;
  background:red;
}

input:valid+span:after {
  position: absolute;
  content: '✓';
  padding-left: 5px;
}

html html

<form novalidate>
  <div>
    <label for="uname">Has No Value: </label>
    <input type="text" id="name1" value='' name="name1" required>
        <span class="validity"></span>
  </div>


  <div>

<label for="uname">Has Value: </label>
    <input type="text" id="name2" value='hello' name="name2" required>
        <span class="validity"></span>
  </div>


  <div>
<label for="uname">Value is too long: </label>
    <input type="text" id="uname3" value='abcdefghijk' name="name3" 
    maxlength=5>
    <span class="validity"></span>
  </div>
  <div>
    <button>Submit</button>
  </div>
</form>

You could additionally use the pattern attribute.您还可以使用pattern属性。 The the pattern attribute checks if the value of an input meets the conditions stated in a RegExp. pattern属性检查输入的值是否满足 RegExp 中规定的条件。

 div { margin-bottom: 10px; position: relative; } input + span { padding-right: 130px; } input:invalid+span:after { position: absolute; content: '✖'; padding-left: 5px; background:red; } input:valid+span:after { position: absolute; content: '✓'; padding-left: 5px; }
 <!-- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/text --> <form novalidate> <div> <label for="uname">Has No Value: </label> <input type="text" id="name1" value='' name="name1" required> <span class="validity"></span> </div> <div> <label for="uname">Has Value: </label> <input type="text" id="name2" value='hello' name="name2" required> <span class="validity"></span> </div> <div> <label for="uname">Value is too long: </label> <input type="text" id="uname3" value='abcdefghijk' name="name3" pattern='.{0,6}' maxlength='6'> <span class="validity"></span> </div> <div> <button>Submit</button> </div> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM