验证表单上的两个字段

时间:2018-07-02 10:58:39

标签: javascript html forms validation

我想知道是否有人可以帮助我验证我的表格。 需要验证的两个字段是门牌号和邮政编码 我只需要把门牌号码设为两个 邮政编码只有5个字符。

我使用了pattern属性,想知道我是否可以在多个字段上显示消息。

<div class="col-md-6 text-center">
<form name="submit-to-google-sheet">
  <label>Address</label>
	<br>
  <input name="address" type="text" placeholder="House Number" pattern="[0-9]{2}">
	<br>
	<br>
  <input name="address2" type="text" placeholder="Street Name">
	<br>
	<br>
  <input name="city" type="text" placeholder="City/Town">
	<br>
	<br>
  <input name="postcode" type="text" placeholder="Postcode" pattern="[0-9]{5}">
	<br>
	<br>
  <label>Plant Type</label>
	<br>
	<select name="plant">
	<option value="tree">TREE</option>
	<option value="shrub">SHRUB</option>
	</select>
	<br>
	<br>
  <label>Description</label>
	<br>
  <textarea input name="description">
  </textarea>
	<br>
	<br>
  <label>Rating</label>
	<br>
<div class="rating">	
<input id="star1" type="radio" name="rating" value="1">
<label for="star1" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

	
<input id="star2" type="radio" name="rating" value="2">
<label for="star2" aria-hidden="true">
<i class="fas fa-star"></i>
</label>	
	
<input id="star3" type="radio" name="rating" value="3">
<label for="star3" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

<input id="star4" type="radio" name="rating" value="4">
<label for="star4" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

<input id="star5" type="radio" name="rating" value="5">
<label for="star5" aria-hidden="true">
<i class="fas fa-star"></i>
</label>
</div>	
<br>
<br>
<button type="submit">SUBMIT</button>
</form>

2 个答案:

答案 0 :(得分:1)

您只需使用html属性maxlength =“ 2”

例如

<div class="col-md-6 text-center">
<form name="submit-to-google-sheet">
  <label>Address</label>
	<br>
  <input name="address" type="text" placeholder="House Number" pattern="[0-9]{2}" maxlength="2">
	<br>
	<br>
  <input name="address2" type="text" placeholder="Street Name">
	<br>
	<br>
  <input name="city" type="text" placeholder="City/Town">
	<br>
	<br>
  <input name="postcode" type="text" placeholder="Postcode" pattern="[0-9]{5}" maxlength="5">
	<br>
	<br>
  <label>Plant Type</label>
	<br>
	<select name="plant">
	<option value="tree">TREE</option>
	<option value="shrub">SHRUB</option>
	</select>
	<br>
	<br>
  <label>Description</label>
	<br>
  <textarea input name="description">
  </textarea>
	<br>
	<br>
  <label>Rating</label>
	<br>
<div class="rating">	
<input id="star1" type="radio" name="rating" value="1">
<label for="star1" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

	
<input id="star2" type="radio" name="rating" value="2">
<label for="star2" aria-hidden="true">
<i class="fas fa-star"></i>
</label>	
	
<input id="star3" type="radio" name="rating" value="3">
<label for="star3" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

<input id="star4" type="radio" name="rating" value="4">
<label for="star4" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

<input id="star5" type="radio" name="rating" value="5">
<label for="star5" aria-hidden="true">
<i class="fas fa-star"></i>
</label>
</div>	
<br>
<br>
<button type="submit">SUBMIT</button>
</form>

答案 1 :(得分:0)

您可以使用此代码

css:-

       input:valid {
          color: green;
       }
     input:invalid {
        color: red;
        }

<div class="col-md-6 text-center">
<form name="submit-to-google-sheet">
  <label>Address</label>
	<br>
  <input name="address" type="text" placeholder="House Number" pattern="\d{2}" required>
	<br>
	<br>
  <input name="address2" type="text" placeholder="Street Name">
	<br>
	<br>
  <input name="city" type="text" placeholder="City/Town">
	<br>
	<br>
  <input name="postcode" type="text" placeholder="Postcode" pattern="\d{5}" required>
	<br>
	<br>
  <label>Plant Type</label>
	<br>
	<select name="plant">
	<option value="tree">TREE</option>
	<option value="shrub">SHRUB</option>
	</select>
	<br>
	<br>
  <label>Description</label>
	<br>
  <textarea input name="description">
  </textarea>
	<br>
	<br>
  <label>Rating</label>
	<br>
<div class="rating">	
<input id="star1" type="radio" name="rating" value="1">
<label for="star1" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

	
<input id="star2" type="radio" name="rating" value="2">
<label for="star2" aria-hidden="true">
<i class="fas fa-star"></i>
</label>	
	
<input id="star3" type="radio" name="rating" value="3">
<label for="star3" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

<input id="star4" type="radio" name="rating" value="4">
<label for="star4" aria-hidden="true">
<i class="fas fa-star"></i>
</label>

<input id="star5" type="radio" name="rating" value="5">
<label for="star5" aria-hidden="true">
<i class="fas fa-star"></i>
</label>
</div>	
<br>
<br>
<button type="submit">SUBMIT</button>
</form>