Advertisement
rdsedmundo

foobar

Nov 17th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.81 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <style>
  5.             label {
  6.                 display: block;
  7.             }
  8.  
  9.             input {
  10.                 display: block;
  11.                 margin-bottom: 10px;
  12.                 outline: none;
  13.                 border: thin solid #ccc;
  14.             }
  15.         </style>
  16.     </head>
  17.     <body>
  18.         <form id="validateForm" novalidate>
  19.             <label for="input-one">First</label>
  20.             <input id="first" type="text" />
  21.  
  22.             <label for="input-one">Second</label>
  23.             <input id="second" type="text" />
  24.  
  25.             <label for="input-one">Three</label>
  26.             <input id="three" type="text" />
  27.  
  28.             <button type="submit">Submit</button>
  29.         </form>
  30.  
  31.         <script type="text/javascript">
  32.             (function () {
  33.                 document.addEventListener('DOMContentLoaded', documentReady);
  34.  
  35.                 function documentReady() {
  36.                     let $form = document.getElementById('validateForm');
  37.  
  38.                     $form.addEventListener('submit', function (event) {
  39.                         event.preventDefault();
  40.                         let $this = this;
  41.  
  42.                         let $inputs = Array.from($this.children).filter((node) => {
  43.                             return node.nodeName === "INPUT";
  44.                         });
  45.  
  46.                         $inputs.forEach((node) => {
  47.                             let value = node.value;
  48.  
  49.                             if (!value || isNaN(value))
  50.                                 node.style.borderColor = 'red';
  51.                             else
  52.                                 node.style.borderColor = 'green';
  53.                         });
  54.                     });
  55.                 }
  56.             })();
  57.         </script>
  58.     </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement