JavaScript HTML5 implements form validation _javascript tips

Source: Internet
Author: User

Form validation is a user experience optimization that detects invalid data and marks these errors for end users.

The following is a display of the browser's own validation features can also be viewed on the mobile side:

HTML section:

<! DOCTYPE html>  

CSS section:

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

  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 to realize the form verification of all the code, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.