Facebook
From Silly Duck, 4 Years ago, written in HTML5.
This paste is a reply to Javascript Validation in HTML5 from Tan Jun Qi - view diff
Embed
Download Paste or View Raw
Hits: 30
  1. <!doctype html>
  2.   <title> Murdoch Registration Form </title>
  3.    
  4.   var letters = /^[A-Za-z]+$/;
  5.   var Numbers = /^[0-9]+$/;
  6.   var Symbols = /[$-/:-?{-~!"^_`\[\]]/;
  7.   var DateFormat = /^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/]\d{4}$/;
  8.   var EmailFormat = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  9.   const form = document.getElementById("form");
  10.   var BatchValidate = 0;
  11.  
  12.  
  13.  
  14.      
  15.     function FirstNameValidation()
  16.     {
  17.    
  18.     var FirstNameInput = document.getElementById("FirstName");
  19.     var CheckEmpty = FirstNameInput.value.length;
  20.     var ErrorElement = document.getElementById("FNError");
  21.     var isEmpty;
  22.     if(CheckEmpty!=0)
  23.     {
  24.  
  25.       isEmpty = false;
  26.     }
  27.     else {
  28.       ErrorElement.innerHTML ="There is no Input";
  29.       isEmpty = true;
  30.     }
  31.     if(!isEmpty)
  32.     {
  33.       if(FirstNameInput.value.match(letters))
  34.           {
  35.               ErrorElement.innerHTML = "Valid Input";
  36.               BatchValidate = 0;
  37.              
  38.           }
  39.         else
  40.             {
  41.                 ErrorElement.innerHTML ="Invalid Input (Numbers or symbols detected)";
  42.                 BatchValidate = 1;
  43.             }
  44.     }
  45.      
  46.     }
  47.    
  48.     function LastNameValidation()
  49.     {
  50.         var LastNameInput = document.getElementById("LastName");
  51.         var CheckEmpty = LastNameInput.value.length;
  52.         var ErrorElement= document.getElementById("LNError")
  53.         var isEmpty;
  54.         if(CheckEmpty!=0)
  55.         {
  56.  
  57.             isEmpty = false;
  58.         }
  59.         else
  60.         {
  61.             ErrorElement.innerHTML = "There is no Input";
  62.             BatchValidate = 1;
  63.             isEmpty = true;
  64.         }
  65.     if(!isEmpty)
  66.     {
  67.       if(LastNameInput.value.match(letters))
  68.           {
  69.               ErrorElement.innerHTML = "Valid Input";
  70.               BatchValidate = 0;
  71.           }
  72.         else
  73.             {
  74.                ErrorElement.innerHTML = "Invalid Input (Numbers of symbol detected)";
  75.                BatchValidate = 1;
  76.             }
  77.     }
  78.     }
  79.    
  80.     function StudentNumberValidation()
  81.     {
  82.         var S_Number = document.getElementById("StudentNumber");
  83.         var CheckEmpty = S_Number.value.length;
  84.         var ErrorElement = document.getElementById("StudentError");
  85.         var isEmpty;
  86.         if(CheckEmpty==0)
  87.             {
  88.                 StudentError.innerHTML="No Input detected";
  89.                 BatchValidate = 1;
  90.                 isEmpty = true;
  91.                
  92.             }
  93.         else
  94.             {
  95.                 isEmpty = false;
  96.             }
  97.         if(!isEmpty && S_Number.value.match(letters))
  98.            {
  99.                ErrorElement.innerHTML="Invalid Input (Letters detected)";
  100.                 BatchValidate = 1;
  101.             }
  102.        
  103.         else if((!isEmpty) && (CheckEmpty>8||CheckEmpty<8) && (S_Number.value.match(Numbers)))
  104.            {
  105.               ErrorElement.innerHTML="Invalid input (more or less than 8 numbers)";
  106.                BatchValidate = 1;
  107.             }
  108.         else if((!isEmpty) && (!S_Number.value.match(Numbers)))
  109.            {
  110.                ErrorElement.innerHTML = "Invalid Input (Letters detected)";
  111.                 BatchValidate = 1;
  112.             }
  113.         else if(!isEmpty && S_Number.value.match(Numbers) && CheckEmpty==8)
  114.            {
  115.               ErrorElement.innerHTML="Valid Input";
  116.                 BatchValidate = 0;
  117.             }
  118.  
  119.     }
  120.    
  121.     function NumberValidation()
  122.     {
  123.         var NumberInput = document.getElementById("PhoneNumber").value;
  124.         var checkEmpty = NumberInput.length;
  125.         var ErrorElement = document.getElementById("PhoneError");
  126.         var Substring = NumberInput.substring(3,12);
  127.         var CheckNumberInput = NumberInput.substr(0,NumberInput.length);
  128.         var isEmpty;
  129.        
  130.         if(checkEmpty==0)
  131.             {
  132.                 ErrorElement.innerHTML ="Please Input a Telephone Number";
  133.                 BatchValidate = 1;
  134.                 isEmpty = true;
  135.             }
  136.         else
  137.             {
  138.                 isEmpty = false;
  139.             }
  140.          
  141.          if(!isEmpty && !NumberInput.startsWith("+65"))
  142.            {
  143.                ErrorElement.innerHTML = "Please add in the prefix (+65)";
  144.                 BatchValidate = 1;
  145.             }
  146.         else if(!isEmpty && NumberInput.startsWith("+65") && CheckNumberInput.length>11 || CheckNumberInput.length<11)
  147.            {
  148.                ErrorElement.innerHTML = "Please enter a 8 number phone number";
  149.                 BatchValidate = 1;
  150.                 for(var i = 0;i<CheckNumberInput.length;i++)
  151.                    {
  152.                        if(CheckNumberInput.charAt(i).match(letters) || Substring.charAt(i).match(Symbols))
  153.                            {
  154.                                ErrorElement.innerHTML = "No letters or Symbols allowed";
  155.                                BatchValidate = 1;
  156.                            }
  157.                    }
  158.                
  159.            }
  160.        
  161.         else if(!isEmpty && NumberInput.startsWith("+65") && Substring.match(Numbers) )
  162.            {
  163.                ErrorElement.innerHTML = "Valid Input";
  164.                BatchValidate = 0;
  165.                
  166.                
  167.            }
  168.    }
  169.    
  170.    function EnrollmentDateValidation()
  171.    {
  172.        
  173.        var DateInput = document.getElementById("EnrollmentDate").value;
  174.        var ErrorElement = document.getElementById("EnrollmentError");
  175.        var CheckEmpty = DateInput.length;
  176.        var isEmpty;
  177.        if(CheckEmpty==0)
  178.            {
  179.                ErrorElement.innerHTML = "Please Input a date";
  180.                BatchValidate = 1;
  181.                isEmpty = true;
  182.            }
  183.        else
  184.            {
  185.                isEmpty = false;
  186.            }
  187.        if(!isEmpty && DateInput.match(DateFormat))
  188.            {
  189.                ErrorElement.innerHTML = "Correct Format";
  190.                BatchValidate = 0;
  191.            }
  192.        else if(!isEmpty && !DateInput.match(DateFormat))
  193.            {
  194.                ErrorElement.innerHTML = "Wrong Format";
  195.                BatchValidate = 1;
  196.            }
  197.    }
  198.    
  199.    function DOBValidation()
  200.    {
  201.        var DateInput = document.getElementById("DateofBirth").value;
  202.        var ErrorElement = document.getElementById("DOBError");
  203.        var CheckEmpty = DateInput.length;
  204.        var isEmpty;
  205.        if(CheckEmpty==0)
  206.            {
  207.                ErrorElement.innerHTML = "Please Input a date";
  208.                BatchValidate = 1;
  209.                isEmpty = true;
  210.            }
  211.        else
  212.            {
  213.                isEmpty = false;
  214.            }
  215.        if(!isEmpty && DateInput.match(DateFormat))
  216.            {
  217.                ErrorElement.innerHTML = "Correct Format";
  218.                BatchValidate = 0;
  219.            }
  220.        else if(!isEmpty && !DateInput.match(DateFormat))
  221.            {
  222.                ErrorElement.innerHTML = "Wrong Format";
  223.                BatchValidate = 1;
  224.            }
  225.    }
  226.    
  227.    function EmailValidation()
  228.    {
  229.        var EmailInput = document.getElementById("Email").value;
  230.        var ErrorElement = document.getElementById("EmailError");
  231.        var CheckEmpty = EmailInput.length;
  232.        var isEmpty;
  233.        if(CheckEmpty==0)
  234.            {
  235.                ErrorElement.innerHTML = "Please Input an Email";
  236.                BatchValidate = 1;
  237.                isEmpty = true;
  238.            }
  239.        else
  240.            {
  241.                isEmpty = false;
  242.            }
  243.        if(!isEmpty && !EmailInput.match(EmailFormat))
  244.            {
  245.                ErrorElement.innerHTML = "Please enter an valid Email";
  246.                BatchValidate = 1;
  247.            }
  248.        else if(!isEmpty && EmailInput.match(EmailFormat))
  249.            {
  250.                ErrorElement.innerHTML = "Valid Input";
  251.                BatchValidate = 0;
  252.            }
  253.    }
  254.    
  255.    function StudyStatusValidate()
  256.    {
  257.        var message = document.getElementById("StatusMsg");
  258.        if(document.getElementById("Study Status").checked)
  259.            {
  260.                message.innerHTML = "InputChecked";
  261.                BatchValidate = 0;
  262.            }
  263.        else
  264.            {
  265.                message.innerHTML = "Please select an option";
  266.                BatchValidate = 1;
  267.            }
  268.    }
  269.    
  270.    function CourseValidaiton()
  271.    {
  272.        var message = document.getElementById("CourseMsg");
  273.        if(document.getElementById("Course").checked)
  274.            {
  275.                message.innerHTML = "InputChecked";
  276.                BatchValidate = 0;
  277.            }
  278.        else
  279.            {
  280.                message.innerHTML = "Please select an option";
  281.                BatchValidate = 1;
  282.            }
  283.    }
  284.    
  285.    function PastTimeValidation()
  286.    {
  287.        var message = document.getElementById("PastTimeMsg");
  288.        if(document.getElementById("Favourite Pastime").checked)
  289.            {
  290.                 message.innerHTML = "InputChecked";
  291.                BatchValidate = 0;
  292.            }
  293.        else
  294.            {
  295.                message.innerHTML = "Please select an option";
  296.                BatchValidate = 1;
  297.            }
  298.    }
  299.    
  300.    
  301.    function BatchValidation()
  302.    {
  303.        FirstNameValidation();
  304.        LastNameValidation();
  305.        NumberValidation();
  306.        StudentNumberValidation();
  307.        EnrollmentDateValidation();
  308.        DOBValidation();
  309.        EmailValidation();
  310.        StudyStatusValidate();
  311.        CourseValidaiton();
  312.        PastTimeValidation();
  313.        
  314.        if(BatchValidate == 0)
  315.            {
  316.                return true;
  317.            }
  318.        else
  319.            {
  320.                return false;
  321.            }
  322.        
  323.        
  324.        
  325.    }
  326.    
  327.    
  328.    
  329.  
  330.  
  331. </head>
  332.  
  333.  
  334.   <h1>Murdoch Singaporean Registration Form</h1>
  335.  
  336.   <hr>
  337.  
  338.   <form name ="form">
  339.  
  340.     <br></br>
  341.  
  342.     <label> First-Name </label>
  343.  
  344.      &nbsp
  345.  
  346.    <input id="FirstName" name="FirstName" type="text" placeholder="FirstName" oninput ="FirstNameValidation()" required/>
  347.    <div id = "FNError"></div>
  348.    <br></br>
  349.  
  350.  
  351.    <label> Last-Name</label>
  352.  
  353.    <input id ="LastName" name="LastName" type="text" placeholder = "LastName" oninput = "LastNameValidation()" required/>
  354.    
  355.    <div id = "LNError"></div>
  356.  
  357.    <br></br>
  358.    
  359.    <label> Student Number</label>
  360.  
  361.    &nbsp
  362.    <input id="StudentNumber" name="StudentNumber" type="Student Number" placeholder="Murdoch Number" oninput = "StudentNumberValidation()"/>
  363.    
  364.    <div id = "StudentError"></div>
  365.    <br></br>
  366.    <label> Phone Number</label>
  367.  
  368.    &nbsp
  369.  
  370.    <input id="PhoneNumber" name="PhoneNumber" type = "text" placeholder="With prefix" oninput = "NumberValidation()"/>
  371.    <div id = "PhoneError"></div>
  372.  
  373.    <br></br>
  374.  
  375.    <label>Enrollment Date </label>
  376.  
  377.    &nbsp
  378.  
  379.    <input id="EnrollmentDate" name="EnrollmentDate" type="text" placeholder="dd/mm/yyyy" oninput = "EnrollmentDateValidation()"/>
  380.    <div id = "EnrollmentError"></div>
  381.  
  382.    <br></br>
  383.  
  384.    <label>Date of Birth</label>
  385.  
  386.    &nbsp
  387.    <input id="DateofBirth" name="DateofBirth" type = "text" placeholder = "dd/mm/yyyy" oninput = " DOBValidation()" />
  388.    <div id= "DOBError"></div>
  389.  
  390.    <br></br>
  391.  
  392.    <label>Email</label>
  393.  
  394.    &nbsp
  395.  
  396.    <input id="Email" name="Email" type="email" placeholder="[email protected]" oninput = "EmailValidation()"/>
  397.    <div id="EmailError"></div>
  398.  
  399.    <br></br>
  400.  
  401.    <label>Study Status</label>
  402.  
  403.    &nbsp
  404.    <input id="Study Status" name="Study Status" type = "radio" value="Full-Time"> Full-Time
  405.    <input id="Study Status" name="Study Status" type ="radio" value ="Part-Time"> Part-Time
  406.    <div id = "StatusMsg"></div>
  407.    <br></br>
  408.    <label> Major </label>
  409.    <select>
  410.      <option> Single Major
  411.      <option> Double Major
  412.      <option> Triple Major
  413.    </select>
  414.    <br></br>
  415.    <label>Course</label>
  416.    &nbsp
  417.    <input id="Course" name="Course" type = "radio" value="Information Technology"> Information Technology
  418.    <input id="Course" name="Course" type ="radio" value ="Business"> Business
  419.    <input id="Course" name="Course" type = "radio" value ="Psycology">Psycology
  420.    <div id = "CourseMsg"></div>
  421.    <br></br>
  422.    <label> Highest Qualification </label>
  423.    &nbsp
  424.    <select>
  425.      <option> Diploma
  426.      <option> Higher Nitec
  427.      <option> Nitec
  428.      <option> A level
  429.      <option> N level
  430.      <option> O level
  431.      <option> PSLE
  432.    </select>
  433.    <br></br>
  434.  
  435.    <label>Favourite Pastime</label>
  436.    &nbsp
  437.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value="Surfing the Web">Surfing the Web
  438.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value="Playing Sport">Playing Sport
  439.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value="Listening to Music">Listening to Music
  440.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value="Watching TV">Watching TV
  441.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value=" Playing Games"> Playing Games
  442.    <br></br>
  443.  
  444.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value=" Community Service"> Community Service
  445.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value=" Daydreaming"> Daydreaming
  446.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value=" Reading"> Reading
  447.    <input id="Favourite Pastime" name = "Favourite Pastime" type ="radio" value=" Meditation"> Meditation
  448.    <div id = "PastTimeMsg"></div>
  449.    <br></br>
  450.    
  451.    <input type = submit value = "Submit Form" onclick = "return BatchValidation()">
  452.    &nbsp
  453.    <input type = reset value = "Clear Form">
  454.  </form>
  455.  
  456. </body>
  457. </html>
captcha