Calvin
Calvin

Reputation: 11

Validation for two fields on a form

I was wondering if anyone could please need help me validate my form. The two fields that need validation are house number and postcode I need to make the house number only two numbers and the postcode only 5 characters.

I've used a pattern attribute and was wondering if I can display the message on more than one field.

<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>

Upvotes: 0

Views: 155

Answers (1)

Rahul
Rahul

Reputation: 1

Simply you can can do it using a html attribute maxlength = "2"

For Example

<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>

Upvotes: 1

Related Questions