fm-ezra6-1

Suresh Kumar - Form Data Validation

Dec 3rd, 2017
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.52 KB | None | 0 0
  1. <html>
  2. <head>
  3.         <title>JavaScript Chapter 7 - Form Data Validation with HandsOn</title>
  4. <style type="text/css">
  5.     .Er{
  6.         color:red
  7.     }
  8.     form th{
  9.         text-align: right;
  10.     }
  11.     .submit_center{
  12.         text-align:center;
  13.     }
  14. </style>
  15. <script type="text/javascript">
  16.     x=document.getElementById("txt_EmpId");
  17.     function fnnumbervalidate(id, lbl){
  18.         no=document.getElementById(id).value;
  19.         x=document.getElementById(lbl);
  20.         if(document.getElementById(id).value!=""){
  21.             if(isNaN(no)==true){
  22.                 x.innerHTML="<span class='Er'>Error: Input should have <b>Numbers Only</b></span>";
  23.             }else{
  24.                 x.innerHTML="";
  25.             }
  26.         }else{
  27.             x.innerHTML="<span class='Er'>This field should have a value</span>";
  28.         }
  29.     }    
  30.     // This can be easily replaced with a placeholder in the HTML and modified with CSS(not yet standardized).  This also tricks the function fnallvalid() as it will contain data if this function is used.
  31.     /*function fnload(){
  32.         document.getElementById("txt_dob").value="dd/mm/yyyy";
  33.         document.frm_emp.dob.style.color="#c0c0c0";
  34.     }*/
  35.     function fnclear(id, lbl){
  36.         document.getElementById(id).value="";
  37.     }
  38.     function fnallvalid(){
  39.         n=document.frm_emp.elements.length;
  40.         for(i=1; i<n-2;i++){
  41.            if(document.frm_emp.elements[i].value==""){
  42.                x=document.getElementById("eid");
  43.                x=document.getElementById(i);
  44.                x.innerHTML="<span class='Er'>Error: #"+i+" is invalid</span>";
  45.             }
  46.         }
  47.     }
  48. </script>
  49. </head>
  50. <body id="blank"> <!-- original content has 'onload="fnload()"' inside bodytag.  See note at fnload() script above.-->
  51. <a href="https://youtu.be/OuCBp4C5NMI">https://youtu.be/OuCBp4C5NMI</a><br /><br />
  52. <form name="frm_emp" id="frm_emp" action="xx.jsp">
  53.     <fieldset>
  54.         <legend>Employee Registration</legend>
  55.         <table>
  56.             <tr>
  57.                 <th>*Employee ID: </th>
  58.                 <td><input type="text" id="txt_EmpId" onblur="fnnumbervalidate('txt_EmpId','1')" onfocus="fnclear('txt_EmpId','1')"></td>
  59.                 <td><div id="1" value=""></div></td>
  60.             </tr>                
  61.             <tr>
  62.                 <th>Employee Name: </th>
  63.                 <td><input type="text" id="txt_Empname"></td>
  64.                 <td><div id="2"></div></td>
  65.             </tr>
  66.             <tr>
  67.                 <th>DoB: </th>
  68.                 <td><input type="text" id="txt_dob" name="dob" onfocus="fnclear('txt_dob','3')" placeholder="dd/mm/yyyy"></td>
  69.                 <td><div id="3"></div></td>
  70.             </tr>
  71.             <tr>
  72.                 <th>Gender: </th>
  73.                 <td><input type="radio" id="rdo_gender" name="rdo_gen">Male<input type="radio" id="Radio1" name="rdo_gen">Female</td>
  74.                 <td><div id="4"></div></td>
  75.             </tr>
  76.             <tr>
  77.                 <th>*Age: </th>
  78.                 <td><input type="text" id="txt_Age" onfocus="fnclear('txt_Age','6')" onblur="fnnumbervalidate('txt_Age','6')"></td>
  79.                 <td><div id="6"></div></td>
  80.             </tr>
  81.             <tr>
  82.                 <th>Address: </th>
  83.                 <td><textarea id="txtarea_Address"></textarea></td>
  84.                 <td><div id="7"></div></td>
  85.             </tr>
  86.             <tr>
  87.                 <th>Qualification: </th>
  88.                 <td>
  89.                     <select id="select_qual">
  90.                         <option>B.E</option>
  91.                         <option>B. Tech</option>
  92.                         <option>B.D</option>
  93.                         <option>BCA</option>
  94.                         <option>MCA</option>
  95.                     </select>
  96.                 </td>
  97.                 <td><div id="8" value=""></div></td><!-- I'm not sure what the author meant to do with the value here but it's unnecessary as far as I can tell -->
  98.             </tr>
  99.             <tr>
  100.                 <th>*Contact Number: </th>
  101.                 <td><input type="text" id="txt_contact" onfocus="fnclear('txt_contact','9')" onblur="fnnumbervalidate('txt_contact','9')"></td>
  102.                 <td><div id="9"></div></td>
  103.             </tr>
  104.             <tr>
  105.                 <th>Area of Interest: </th>
  106.                 <td><input type="text" id="txt_interest"></td>
  107.                 <td><div id="10"></div></td>
  108.             </tr>
  109.             <tr>
  110.                 <th>*Years of Experience</th>
  111.                 <td><input type="text" id="txt_exp" onblur="fnnumbervalidate('txt_exp','11')" onfocus="fnclear('txt_exp','11')"></td>
  112.                 <td><div id="11"></div></td>
  113.             </tr>
  114.         </table>
  115.     </fieldset>
  116.     <center><!-- center is an outdated tag and does not work!!  "class=submit_center" has been added to the <td> tag below-->
  117.         <table width="100%">
  118.             <col align="right">
  119.             <tr>
  120.                 <td class="submit_center">
  121.                     <input type="button" id="submit" value="Submit" onclick="fnallvalid()"><!-- "type=submit" will do as instructed but won't stop passing the form action.  as "type=button" you can see the errors. -->
  122.                     <input type="button" onclick="document.getElementById('frm_emp').reset();" id="clear" value="Clear"><!-- the authors way of clearing still submits the form and I can't tell if it clears the form before submitting.  See submit button note. -->
  123.                 </td>                
  124.             </tr>
  125.         </table>
  126.     </center>
  127. </form>
  128. </body>
  129. </html>
Advertisement
Add Comment
Please, Sign In to add comment