Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JavaScript Document
- function formvalidation(form){
- var Email = form.email.value;
- var ConfirmEmail = form.confirmemail.value;
- var dob = form.dob.value;
- var PhoneNumber = form.phonenumber.value;
- var password = form.password.value;
- // Regular Expression code
- var EmailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,})+$/;
- var inputs = document.getElementsByTagName('input'); // Gets all the inputs and related attributes (id,name,value etc)
- 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
- if (inputs[i].value == "") { // Goes through each input
- 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
- } else {
- document.getElementById(inputs[i].name+"error").innerHTML = ""; // Clear it if user fixs it
- }
- }
- if (Email != "") { // Covered by blank checker, so it checks that it hasn't been there first
- if ( !(EmailRegEx.test(Email) ) ) {
- document.getElementById("emailerror").innerHTML = "Please enter a valid email.";
- } else {
- document.getElementById("emailerror").innerHTML = ""; // Clear it if user fixs it
- }
- }
- if (ConfirmEmail != "") { // Covered by blank checker, so it checks that it hasn't been there first
- if (Email != ConfirmEmail) {
- document.getElementById("confirmemailerror").innerHTML = "Please enter the same email as before.";
- } else {
- document.getElementById("confirmemailerror").innerHTML = ""; // Clear it if user fixs it
- }
- }
- if (password.length < 5) {
- document.getElementById("passworderror").innerHTML = "Password must be 6 characters or more.";
- if (password == "") {
- document.getElementById("passworderror").innerHTML = "Please enter a password.";
- }
- form.password.value = "";
- } else {
- document.getElementById("passworderror").innerHTML = ""; // Clear it if user fixs it
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement