Advertisement
JordsAus

Untitled

Oct 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. // JavaScript Document
  2. function formvalidation(form){
  3. var Email = form.email.value;
  4. var ConfirmEmail = form.confirmemail.value;
  5. var dob = form.dob.value;
  6. var PhoneNumber = form.phonenumber.value;
  7. var password = form.password.value;
  8.  
  9. // Regular Expression code
  10. var EmailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,})+$/;
  11.  
  12.  
  13. var inputs = document.getElementsByTagName('input'); // Gets all the inputs and related attributes (id,name,value etc)
  14. for (var i = 0; i <= inputs.length - 5; i++) { // Goes through all the input except password and radio has it own handler that's why it's minus 5
  15. if (inputs[i].value == "") { // Goes through each input
  16. document.getElementById(inputs[i].name+"error").innerHTML = "Please enter " + inputs[i].id + "."; // The id is used to make it user friendly error message the name is used for identification
  17.  
  18. } else {
  19. document.getElementById(inputs[i].name+"error").innerHTML = ""; // Clear it if user fixs it
  20.  
  21. }
  22. }
  23.  
  24.  
  25. if (Email != "") { // Covered by blank checker, so it checks that it hasn't been there first
  26. if ( !(EmailRegEx.test(Email) ) ) {
  27. document.getElementById("emailerror").innerHTML = "Please enter a valid email.";
  28. } else {
  29. document.getElementById("emailerror").innerHTML = ""; // Clear it if user fixs it
  30. }
  31. }
  32.  
  33. if (ConfirmEmail != "") { // Covered by blank checker, so it checks that it hasn't been there first
  34. if (Email != ConfirmEmail) {
  35. document.getElementById("confirmemailerror").innerHTML = "Please enter the same email as before.";
  36. } else {
  37. document.getElementById("confirmemailerror").innerHTML = ""; // Clear it if user fixs it
  38. }
  39. }
  40.  
  41. if (password.length < 5) {
  42. document.getElementById("passworderror").innerHTML = "Password must be 6 characters or more.";
  43. if (password == "") {
  44. document.getElementById("passworderror").innerHTML = "Please enter a password.";
  45. }
  46. form.password.value = "";
  47. } else {
  48. document.getElementById("passworderror").innerHTML = ""; // Clear it if user fixs it
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement