首页 > 语言 > JavaScript > 正文

一个检测表单数据的JavaScript实例

2024-05-06 16:10:18
字体:
来源:转载
供稿:网友

这篇文章主要介绍了一个检测表单数据的JavaScript实例,很简单,很实用,比较适合初学者

一个检测表单数据的JavaScript实例,很简单,很实用,感兴趣的朋友可以看看

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  5. <title>每天一个JavaScript实例-检测表单数据</title>  
  6. <style>  
  7. [role="alert"]{  
  8. background-color: #fcc;  
  9. font-weight: bold;  
  10. padding:5px;  
  11. border:1px dashed #000;  
  12. }  
  13. div{  
  14. margin:10px 0;  
  15. padding:5px;  
  16. width:400px;  
  17. background-color: #fff;  
  18. }  
  19. </style>  
  20.  
  21. <script>  
  22. window.onload = function(){  
  23. document.getElementById("thirdfield").onchange = validateField;  
  24. document.getElementById("firstfield").onblur = mandatoryField;  
  25. document.getElementById("testform").onsubmit = finalCheck;  
  26. }  
  27. function validateField(){  
  28. removeAlert();  
  29. if(!isNaN(parseFloat(this.value))){  
  30. resetField(this);  
  31. }else{  
  32. badField(this);  
  33. generateAlert("You entered an invalid value in Third Field. only numeric values such as 105 or 3.45 are allowed");  
  34. }  
  35. }  
  36. function removeAlert(){  
  37. var msg = document.getElementById("msg");  
  38. if(msg){  
  39. document.body.removeChild(msg);  
  40. }  
  41. }  
  42. function resetField(elem){  
  43. elem.parentNode.setAttribute("style","background-color:#fff");  
  44. var valid = elem.getAttribute("aria-invalid");  
  45. if(valid) elem.removeAttribute("aria-invalid");  
  46. }  
  47. function badField(elem){  
  48. elem.parentNode.setAttribute("style","background-color#fee");  
  49. elem.setAttribute("aria-invalid","true");  
  50. }  
  51. function generateAlert(txt){  
  52. var txtNd = document.createTextNode(txt);  
  53. msg = document.createElement("div");  
  54. msg.setAttribute("role","alert");  
  55. msg.setAttribute("id","msg");  
  56. msg.setAttribute("class","alert");  
  57.  
  58. msg.appendChild(txtNd);  
  59. document.body.appendChild(msg);  
  60. }  
  61.  
  62. function mandatoryField(){  
  63. removeAlert();  
  64. if(this.value.length > 0 ){  
  65. resetField(this);  
  66. }else{  
  67. badField(this);  
  68. generateAlert("You must enter a value into First Field");  
  69. }  
  70. }  
  71. function finalCheck(){  
  72. //console.log("aaa");  
  73. removeAlert();  
  74.  
  75. var fields =document.querySelectorAll('input[aria-invalid="true"]');  
  76. //var fields =document.querySelectorAll("input[aria-invalid='true']");//错误!!!  
  77. console.log(fields);  
  78. if(fields.length > 0){  
  79. generateAlert("You have incorrect fields entries that must be fixed before you can submit this form");  
  80. return false;  
  81. }  
  82. }  
  83. </script>  
  84.  
  85. </head>  
  86.  
  87. <body>  
  88. <form id = "testform">  
  89. <div>  
  90. <label for="firstfield">*first Field:</label><br />  
  91. <input id="firstfield" name = "firstfield" type = "text" aria-required = "true" />  
  92. </div>  
  93.  
  94. <div>  
  95. <label for="secondfield">Second Field:</label><br />  
  96. <input id="secondfield" name = "secondfield" type = "text" />  
  97. </div>  
  98.  
  99. <div>  
  100. <label for="thirdfield">Third Field(numeric):</label><br />  
  101. <input id="thirdfield" name = "thirdfield" type = "text" />  
  102. </div>  
  103.  
  104. <div>  
  105. <label for="fourthfield">Fourth Field:</label><br />  
  106. <input id="fourthfield" name = "fourthfield" type = "text" />  
  107. </div>  
  108.  
  109. <input type="submit" value = "Send Data" />  
  110. </form>  
  111.  
  112. </body>  
  113. </html> 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选