Advertisement
Patrikrizek

lesson-8-form-short

Jun 22nd, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.78 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>JavaScript Validation Form</title>
  8.  
  9. </head>
  10. <body>
  11.     <!-- Header -->
  12.     <header>
  13.         <h1>Oder and Validation From</h1>
  14.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Maiores, adipisci assumenda eum deleniti provident sapiente magnam enim repellat autem qui.</p>
  15.     </header>
  16.  
  17.  
  18.     <!-- Main -->
  19.     <div id="main">
  20.         <!-- Form - Customer Details -->
  21.         <h2>Customer Details</h2>
  22.         <form>
  23.             <label for="fname">First Name*</label>
  24.             <br>
  25.             <input type="text" name="fname" id="fname" placeholder="John" pattern="[A-Za-z]" required>
  26.             <br>
  27.  
  28.             <label for="phone">Phone Number</label>
  29.             <br>
  30.             <input type="number" name="phone" id="phone" placeholder="012345679" pattern="[0-9]{9}">
  31.             <br>
  32.  
  33.             <label for="email">Email address*</label>
  34.             <br>
  35.             <input type="email" name="email" id="email" placeholder="john.smith@email.com" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" required>
  36.             <br>
  37.  
  38.             <!-- The JavaScript function "validateCustomerDetails()" is called when the submit button clicked. -->
  39.             <button type="button" onclick="validateCustomerDetails()">Submit</button>
  40.             <br>
  41.  
  42.             <!-- JS -->
  43.             <script>
  44.                 // Here the function "validateCustomerDetails()" starts.
  45.                 function validateCustomerDetails() {
  46.  
  47.                     // First we need to save the values from the form into variables.
  48.                     // Find values of relevant elements according theirs IDs and save them in the variables.
  49.  
  50.                     // Search trough the document and find an element with the ID "fname" and save its value to variable named "var fname".
  51.                     var fname = document.getElementById("fname").value;
  52.                     var phone = document.getElementById("phone").value;
  53.                     var email = document.getElementById("email").value;
  54.                    
  55.                     // Regexr Patterns
  56.                     // Here we storing a correct input formatting for each individual form field.
  57.                     var fNameCheckPattern = /^[a-zA-Z]+$/;
  58.                     var phoneCheckPattern =  /[0-9]{9}/;
  59.                     var emailCheckPattern = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/;
  60.  
  61.                     // Validation if statements
  62.                     // Here we are checking if the patterns and value from form fields meeting criteria.
  63.                     // Test if variable "fname" is NOT matching the pattern "fNameCheckPattern". If NOT then search entire document and find element with ID "fname" focus on it and display the alert message "Only characters are allowed."
  64.                     if(!fNameCheckPattern.test(fname)) { document.getElementById("fname").focus(); alert("Only characters are allowed."); }
  65.                    
  66.                     else if (!phoneCheckPattern.test(phone)) { document.getElementById("phone").focus(); alert("Only digits are allowed. Example of format: 0132465798"); }
  67.  
  68.                     else if (!emailCheckPattern.test(email)) { document.getElementById("email").focus(); alert("Input correct email. Example: John.smith@email.com"); }
  69.  
  70.                     else {
  71.                         // If all the inputs from form field are correct call function named "printCustomerDetails()".
  72.                         printCustomerDetails();
  73.                     }
  74.  
  75.                     // Output the result
  76.                     // Here we are creating function named "printCustomerDetails()"
  77.                     function printCustomerDetails() {
  78.                         // Search trough entire document and find an element with ID "outputFullName". Once find insert HTML code in it "<b>Full Name: </b>" plus value of the variable "fullName."
  79.                         document.getElementById("outputName").innerHTML = "<b>Name: </b>" + fname;
  80.                         document.getElementById("outputPhone").innerHTML = "<b>Phone: </b>" + phone;
  81.                         document.getElementById("outputEmail").innerHTML = "<b>Email: </b>" + email;
  82.                     }
  83.  
  84.                 }
  85.  
  86.             </script>
  87.         </form>
  88.  
  89.         <!-- Form - Expedition Details -->
  90.  
  91.         <!-- Printed Forms -->
  92.         <div id="print">
  93.             <h2>Your display output</h2>
  94.  
  95.             <h3>Customer Details</h3>
  96.             <p id="outputName">Full name:</p>
  97.             <p id="outputPhone">Phone:</p>
  98.             <p id="outputEmail">Email:</p>
  99.         </div>
  100.  
  101.     </div>
  102.  
  103. </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement