Advertisement
haikelfazzani

How To Validate Input Form Using Javascript

Jul 21st, 2017
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.01 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset='utf-8'>
  5.         <title>Input Field Validation</title>
  6.     </head>
  7.    
  8.     <body>
  9.         <p>Only numbers Letters And White space</p>
  10.         <input type='text' onblur='inputValidation(this)'> <!-- Our Text Input Filed -->
  11.         <br />
  12.         <div id='textField'></div>
  13.     </body>
  14.    
  15.     <script>
  16.     function inputValidation(inputTxt){
  17.    
  18.         var regx = /^[0-9a-zA-Z ]+$/;
  19.         var textField = document.getElementById("textField");
  20.            
  21.         if(inputTxt.value != '' ){
  22.        
  23.             if(inputTxt.value.length >= 5){
  24.            
  25.                 if(inputTxt.value.match(regx)){
  26.                     textField.textContent = 'Good input';
  27.                     textField.style.color = "green";
  28.                        
  29.                 }else{
  30.                     textField.textContent = 'only numbers, letters And White space';
  31.                     textField.style.color = "red";
  32.                 }  
  33.             }else{
  34.                 textField.textContent = 'your input is less than 5 chracters';
  35.                 textField.style.color = "red";
  36.             }  
  37.         }else{
  38.             textField.textContent = 'your input is empty';
  39.             textField.style.color = "red";
  40.         }
  41.     }
  42.     </script>
  43. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement