validationOrder.js

דף זה הוא מסוג Javascript ומטרתו לבצע בדיקות תקינות לטופס ההרשמה.
 avatar
user_2706180
javascript
a month ago
9.0 kB
1
Indexable
Never
function validationOrder() {
    var res = true;
    res = usernameValidate() && res;
    res = passwordValidate() && res;
    res = passwordCompare() && res;
    res = phoneNumberValidate() && res;
    res = emailValidate() && res;
    res = countryValidate() && res;
    res = firstnameValidate() && res;
    res = lastnameValidate() && res;
    res = genderValidate() && res;
    res = goalKeeperValidate() && res;
    res = defenderValidate() && res;
    res = midfielderValidate() && res;
    res = forwardValidate() && res;
    res = dateValidate() && res;

    return res;
}

function emailValidate() {
    const email = document.getElementById("Email").value;
    const emailError = document.getElementById("Email_ErrorMSG");
    emailError.innerHTML = "";

    const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

    if (!regex.test(email)) {
        emailError.innerHTML = "Enter an Email!";
        return false;
    }

    return true;
}




function usernameValidate() {
    const username = document.getElementById("Username").value;
    const usernameError = document.getElementById("Username_ErrorMSG");
    usernameError.innerHTML = "";

    if (username.length === 0) {
        usernameError.innerHTML = "Enter a username!";
        return false;
    }

    if (username.length < 3) {
        usernameError.innerHTML = "Username must be longer than 3 characters!";
        return false;
    }

    if (username.includes(" ")) {
        usernameError.innerHTML = "Username cannot contain spaces!";
        return false;
    }

    const englishLettersOnly = /^[A-Za-z]+$/;
    if (!englishLettersOnly.test(username)) {
        usernameError.innerHTML = "Username can only contain English letters!";
        return false;
    }

    return true;
}

function firstnameValidate() {
    const username = document.getElementById("FirstName").value;
    const usernameError = document.getElementById("Firstname_ErrorMSG");
    usernameError.innerHTML = "";

    if (username.length === 0) {
        usernameError.innerHTML = "Name cannot be empty!";
        return false;
    }
    else if (username.length < 2) {
        usernameError.innerHTML = "Name must be at least 2 characters!";
    }
    else if (username.includes(" ")) {
        usernameError.innerHTML = "Name cannot contain spaces!";
        return false;
    }

    const englishLettersOnly = /^[A-Za-z]+$/;
    if (!englishLettersOnly.test(username)) {
        usernameError.innerHTML = "Username can only contain English letters!";
        return false;
    }

    return true;
}

function lastnameValidate() {
    const username = document.getElementById("LastName").value;
    const usernameError = document.getElementById("Lastname_ErrorMSG");
    usernameError.innerHTML = "";

    if (username.length === 0) {
        usernameError.innerHTML = "Name cannot be empty!";
        return false;
    }
    else if (username.length < 2) {
        usernameError.innerHTML = "Name must be at least 2 characters!";
    }
    const englishLettersOnly = /^[A-Za-z]+$/;
    if (!englishLettersOnly.test(username)) {
        usernameError.innerHTML = "Username can only contain English letters!";
        return false;
    }

    return true;
}



function passwordValidate() {
    const password = document.getElementById("password").value;
    const passwordError = document.getElementById("Password_ErrorMSG");
    passwordError.innerHTML = "";

    
    if (password.includes(" ")) {
        passwordError.innerHTML = "Password cannot contain spaces!";
        return false;
    }

  
    if (password.length < 6) {
        passwordError.innerHTML = "Password must be at least 6 characters long!";
        return false;
    }

    
    const numberSpecialCharRegex = /(?=.*\d)(?=.*[!@#$%^&*])/;
    if (!numberSpecialCharRegex.test(password)) {
        passwordError.innerHTML = "Password must contain at least one number and one special character!";
        return false;
    }

    
    const uppercaseEnglishCharRegex = /^(?=.*[A-Z])[A-Za-z\d!@#$%^&*]*$/;
    if (!uppercaseEnglishCharRegex.test(password)) {
        passwordError.innerHTML = "Password must include one uppercase letter and only contain English letters.";
        return false;
    }

    return true;
}






function passwordCompare() {
    const confirmPassword = document.getElementById("ConfirmPassword").value;
    const confirmPasswordError = document.getElementById("ConfirmPassword_ErrorMSG");
    const password = document.getElementById("password").value;
    confirmPasswordError.innerHTML = "";

    if (confirmPassword !== password) {
        confirmPasswordError.innerHTML = "The passwords do not match!";
        return false;
    }
    return true;
}

function phoneNumberValidate() {
    const phoneNumber = document.getElementById("PhoneNumber").value;
    const phoneNumberError = document.getElementById("PhoneNumber_ErrorMSG");
    const phoneDigits = /^0(5[0-9])(-?\d{3})(-?\d{4})$/;
    phoneNumberError.innerHTML = "";

    const cleanedPhoneNumber = phoneNumber.replace(/-/g, '');
    if (cleanedPhoneNumber.length !== 10 || !phoneDigits.test(phoneNumber)) {
        phoneNumberError.innerHTML = "Enter a valid phone number!";
        return false;
    }
    return true;
}
function countryValidate() {
    const country = document.getElementById("Country");
    const countryError = document.getElementById("Country_ErrorMSG");
    var selectedOption = country.options[country.selectedIndex].value;
    countryError.innerHTML = "";

    if (selectedOption === "") {
        countryError.innerHTML = "Select a country!";
        return false;
    }
    return true;
}




function genderValidate() {
    
    const genderMale = document.getElementById("male");
    const genderFemale = document.getElementById("female");
    const genderOther = document.getElementById("other");

    
    const genderError = document.getElementById("Gender_ErrorMSG");

    
    genderError.innerHTML = "";

    
    if (!genderMale.checked && !genderFemale.checked && !genderOther.checked) {
        genderError.innerHTML = "Select a gender!"; 
        return false; //
    } else if (genderOther.checked) {
        genderError.innerHTML = "Select a valid gender!";
        return true; 
    }

    return true; 
}


function dateValidate() {
    const dateInput = document.getElementById("BirthDate");
    const dateError = document.getElementById("BirthDate_ErrorMSG");
    const dateValue = dateInput.value;
    dateError.innerHTML = "";

    if (dateValue === "") {
        dateError.innerHTML = "Enter a date!";
        return false;
    }

    const enteredDate = new Date(dateValue);
    const minDate = new Date(1960, 0, 1); 
    const maxDate = new Date();

    if (isNaN(enteredDate.getTime())) {
        dateError.innerHTML = "Enter a datet!";
        return false;
    }

    if (enteredDate < minDate) {
        dateError.innerHTML = "Date must be valid!";
        return false;
    }

    if (enteredDate > maxDate) {
        dateError.innerHTML = "Date must be valid!";
        return false;
    }

    return true;
}

function goalKeeperValidate() {
    const goalKeeper =document.getElementById("GoalKeeper");
    const goalKeeperError = document.getElementById("GoalKeeper_ErrorMSG");
    var selectedOption = goalKeeper.options[goalKeeper.selectedIndex].value;
    goalKeeperError.innerHTML = "";

    if (selectedOption === "") {
        goalKeeperError.innerHTML = "Select a player!";
        return false;
    }
    return true;
}



function defenderValidate() {
    const defender = document.getElementById("Defender");
    const defenderError = document.getElementById("Defender_ErrorMSG");
    var selectedOption = defender.options[defender.selectedIndex].value;
    defenderError.innerHTML = "";

    if (selectedOption === "") {
        defenderError.innerHTML = "Select a player!";
        return false;
    }
    return true;
}

function midfielderValidate() {
    const midfielder = document.getElementById("Midfielder");
    const midfielderError = document.getElementById("Midfielder_ErrorMSG");
    var selectedOption = midfielder.options[midfielder.selectedIndex].value;
    midfielderError.innerHTML = "";

    if (selectedOption === "") {
        midfielderError.innerHTML = "Select a player!";
        return false;
    }
    return true;
}

function forwardValidate() {
    const forward = document.getElementById("Forward");
    const forwardError = document.getElementById("Forward_ErrorMSG");
    var selectedOption = Forward.options[forward.selectedIndex].value;
    forwardError.innerHTML = "";

    if (selectedOption === "") {
        forwardError.innerHTML = "Select a player!";
        return false;
    }
    return true;
}
Leave a Comment