Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with unobtrusive Javascript form validation: write unobtrusive javascr

ID: 3577127 • Letter: N

Question

Need help with unobtrusive Javascript form validation:

write unobtrusive javascript code that does form validation for the above form making sure that usernmane is only text characters, BannerID is exactly 9 digits and email has text followed by @ sign followed by more text with atleast 1 dot in it. your javascript code should register itself and check the fields when the user changes them.

here is php form.

<form id="infoForm" action="whatever.php">

<p>

your user name:

<input type="text" id="username" name="usernae" size="30' />

your banner ID:

<input type="text" id=="Banner" name="Banner" size="9" />

<input type="text" is="email" name="email" size="30" />

<input type="submit" name="Submit" />

</p>

</form>

Explanation / Answer

Javascript form validation:-

<html>
<head>   
   <script>
       function validateForm()
       {
           var x = document.forms["infoForm"]["email"].value;
            var atpos = x.indexOf("@");
            var dotpos = x.lastIndexOf(".");
            if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length )
           {
                   alert("Not a valid e-mail address");
                   return false;
           }
           var input=document.forms["infoForm"]["Banner"].value;
           if (input.length!=9)
           {
               alert("The Banner id must contain 9 letters!");
                return false;
           }
          
           var a=document.forms["infoForm"]["username"].value;
           var letters = /^[A-Za-z]+$/;
           if(a.match(letters))
           {
               return true;
           }
           else
           {
               alert("usernmane is only albhabet text characters");
               return false;
           }
          
       }

   </script>
</head>
<body>
   <form id="infoForm" action="whatever.php" method="post">
   <table>
   <tr>      
       <td>your user name :</td>
       <td><input type="text" id="username" name="username" size="20" /></td>
   </tr>
   <tr>
       <td>your banner ID:</td>
       <td><input type="text" id="Banner" name="Banner" size="9" /></td>
   </tr>
   <tr>
       <td>EmailID:</td>
       <td><input type="text" id="email" name="email" size="30" /></td>
   </tr>
   </table>
   <input type="submit" name="Submit" />
   </form>
</body>
</html>

Note :- As per your requirments this code works perfectly if you leave email field empty it takes input.The email fiels worked for validating text followed by @ sign followed by more text with atleast 1 dot in it.

Thank you!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote