Advertisement
Shailrshah

JavaScript Form Validation

Apr 15th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3.     <script language = "javascript">
  4.         function validate(){
  5.             var fname = document.forms["coursereg"]["fname"].value;
  6.             if(fname == null || fname == ""){
  7.                 alert("Enter your name.")
  8.                 return false;
  9.             }
  10.             var age = parseInt(document.forms["coursereg"]["age"].value);
  11.             if(isNaN(age)){
  12.                 alert("Enter a valid age.");
  13.                 return false;
  14.             }
  15.             if(age < 18 || age > 150){
  16.                 alert("Your age must be in between 18 to 150 to register.");
  17.                 return false;
  18.             }
  19.             var email = document.forms["coursereg"]["email"].value;
  20.             if(email.indexOf("@")<1 || email.lastIndexOf(".")<email.indexOf("@")+2 || email.lastIndexOf(".")+2 >= email.length){
  21.                 alert("Enter a valid email address.");
  22.                 return false;
  23.             }
  24.             alert("Success!");
  25.         }
  26.     </script>
  27. </head>
  28. <body>
  29.     <form name = "coursereg" onsubmit="return validate()" method = "post">
  30.         <p>Name<input name = "fname" type = "text" />
  31.         <p>Age<input name = "age" type = "text" />
  32.         <p>Email<input name = "email" type = "text" />
  33.         <p><input name = "submit" type = "submit">
  34. </body>
  35. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement