A JavaScript instance for detecting form data

Source: Internet
Author: User
This article mainly introduces a JavaScript instance for detecting form data. It is very simple and practical. It is suitable for beginners to use a JavaScript instance for detecting form data. It is very simple and practical. If you are interested, you can check it out.

   
  One JavaScript instance per day-Detects form data Script window. onload = function () {document. getElementById ("thirdfield "). onchange = validateField; document. getElementById ("firstfield "). onblur = mandatoryField; document. getElementById ("testform "). onsubmit = finalCheck;} function validateField () {removeAlert (); if (! IsNaN (parseFloat (this. value) {resetField (this);} else {badField (this); generateAlert ("You entered an invalid value in Third Field. only numeric values such as 105 or 3.45 are allowed ");} function removeAlert () {var msg = document. getElementById ("msg"); if (msg) {document. body. removeChild (msg) ;}} function resetField (elem) {elem. parentNode. setAttribute ("style", "background-color: # fff"); var vali D = elem. getAttribute ("aria-invalid"); if (valid) elem. removeAttribute ("aria-invalid");} function badField (elem) {elem. parentNode. setAttribute ("style", "background-color # background"); elem. setAttribute ("aria-invalid", "true");} function generateAlert (txt) {var txtNd = document. createTextNode (txt); msg = document. createElement ("p"); msg. setAttribute ("role", "alert"); msg. setAttribute ("id", "msg"); msg. setAtt Rigoal ("class", "alert"); msg. appendChild (txtNd); document. body. appendChild (msg);} function mandatoryField () {removeAlert (); if (this. value. length> 0) {resetField (this) ;}else {badField (this); generateAlert ("You must enter a value into First Field") ;}} function finalCheck () {// console. log ("aaa"); removeAlert (); var fields = document. querySelectorAll ('input [aria-invalid = "true"] '); // var fields = d Ocument. querySelectorAll ("input [aria-invalid = 'true']"); // Error !!! Console. log (fields); if (fields. length> 0) {generateAlert ("You have incorrect fields entries that must be fixed before you can submit this form"); return false;} script        

For more articles about JavaScript instances used to detect form data, refer to PHP!

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.