javascript html5 implements form validation

  • 2021-01-18 06:16:24
  • OfStack

Form validation, which detects invalid data for the end user and flags these errors, is a user experience optimization.

The following shows the browser's built-in verification function can also be viewed on the mobile end:

HTML parts:


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
  <title>html5  Form validation </title>
</head>
<body>
<form action="#" id="formValid" class="myform" novalidate="novalidate" onsubmit="return checkForm()">
  <fieldset>
    <div class="form-group">
      <label for="name"> The name of the </label>
      <div>
        <input type="text" class="form-control" id="name" name="name" required/>
        <span class="form-error"> Can't be empty </span>
      </div>
    </div>
    <div class="form-group">
      <label for="email"> email </label>
      <div>
        <input type="email" class="form-control" id="email" name="email" required/>
        <span class="form-error"> The mailbox format is incorrect </span>
      </div>
    </div>
    <div class="form-group">
      <label> provinces </label>
      <select name="province" class="form-control">
        <option value=""> Please select a </option>
        <option value="s">4 sichuan </option>
        <option value="c"> chongqing </option>
      </select>
    </div>
    <input type="submit" class="btn" value=" submit "/>
  </fieldset>
</form>
</body>
</html>

CSS parts:


   fieldset{border: 0;}
  .myform .form-control{
    display: block;
    padding: 5px;
    width: 100%
  }
  .myform input:focus:invalid + .form-error{
    display: inline;
  }
  .myform .form-error{
    display: none;
    position: absolute; 
    margin-top: .7em;
    padding: 1px 2px;
    color: #fff;
    font-size: .875rem;
    background: #f40;
  }
  .myform .form-error:after{
    position: absolute;
    content: "";
    top: -.5em;
    left: .5em;
    z-index: 100;
    display: inline-block;
    width: 0;
    height: 0;
    vertical-align: middle;
    border-bottom: .5em solid #f40;
    border-right: .5em solid transparent;
    border-left: .5em solid transparent;
    border-top: 0 dotted;
    transform: rotate(360deg);
    overflow: hidden;
  }
  .btn{
    padding: 5px 20px;
   }

JavaScript parts:


  function checkForm(){
    var myform = document.getElementById("formValid");
    return check(myform.elements);
  }
  function check(eles){
    var ele;
    for(var i = 0;i<eles.length;i++){
      ele = eles[i];
      if(ele.nodeName == "SELECT"){
        if(!ele.selectedIndex){
          alert(" Please select province ");
          return false;
        }
      }else if(ele.name){
        if(!ele.checkValidity()){
          ele.focus();
          return false;
        }
      }
    }
    return true;
  }

The above is javascript combined with html5 form validation of all the code, I hope to help you learn.


Related articles: