'Do I have a problem with my javascript or is it something else?

(I would like to adress that English is not my first language) I have this problem with javascript for a very long time and I don't know what to do. This javascript is for a registration. Sometimes it gives access even though I haven't typed everything, or it doesn't give access even though I have typed everything correctly If someone can help thanks already!

function validateform() {
    var res = true;
    res = userNameVal() && res;
    res = passowrdVal() && res;
    res = ConfirmPhone() && res;
    res = emailConfirm() && res;
    res = Name() && res;
    res = lastName() && res;
    res = city() && res;
    return res;
}


function Name() {
    var firstName = document.getElementById("firstName").value;
    var msgBox = document.getElementById("NameMsg");
    if (firstName.length == 0) {
        msgBox.innerHTML = "You must enter your name";
        return false;
    }
    var reg = /[0-9]/;
    var reg1 = /\w/;
    var reg2 = /\s/;
    if (reg.test(firstName) && reg1.test(firstName) && reg2.test(firstName) && (English(firstName))) {
        msgBox.innerHTML = "Your name can't have a number, space or a special char";
        return false;
    }
    msgBox.innerHTML = "";
    return true;
} 
function lastName() {
    var LastName = document.getElementById("LastName").value;
    var msgBox = document.getElementById("LastNameMsg");
    var reg = /[0-9]/;
    var reg1 = /\w/;
    var reg2 = /\s/;
    if (Name.length == 0) {
        msgBox.innerHTML = "You must enter your name";
        return false;
    }
    if (reg.test(LastName) || reg1.test(LastName) || reg2.test(LastName)) {
        msgBox.innerHTML = "Your name can't have a number, space or a special char";
        return false;
    }
    msgBox.innerHTML = "";
    return true;
} 
    function city() {
        var CityName = document.getElementById("CityName").value;
        var msgBox = document.getElementById("CityNameMsg");
        var reg = /[0-9]/;
        var reg1 = /\w/;
        var reg2 = /\s/;
        if (CityName.length == 0) {
            msgBox.innerHTML = "You must enter your City";
            return false;
        }
        if (reg.test(CityName) || reg1.test(CityName) || reg2.test(CityName)) {
            msgBox.innerHTML = "Your name can't have a number, space or a special char";
            return false;
        }
        
        msgBox.innerHTML = "";
        return true;
    }

    function userNameVal() {
        var userName = document.getElementById("userName").value;
        var msgBox = document.getElementById("userNameMsg");
        if (userName.length == 0) {
            msgBox.innerHTML = "You must enter a username";
            return false;
        }
        if (!isLetter(userName[0])) {
            msgBox.innerHTML = "Your username must start with a letter";
            return false;
        }
        msgBox.innerHTML = "";
        return true;
    }



    function passowrdVal() {
        var pass = document.getElementById("password").value;
        var msgBox = document.getElementById("passwordMsg");
        var specialChar = /[@!#$%^&*()-+]/;
        if (pass.length == 0) {
            msgBox = "You must enter a password";
            return false;
        }
        if (pass.length < 7) {
            msgBox.innerHTML = "The password must contain at least 7 charactes"
            return false;
        }
        
        if (!specialChar.test(pass)) {
            msgBox.innerHTML = "password must contain one special letter ";
            return false;
        }
        msgBox.innerHTML = "";
        return true;
    }



    function ConfirmPhone() {
        var phone = document.getElementById("phone").value;
        var msgBox = document.getElementById("phoneMsg");
        var reg = /^0{1}(2|3|4|6|8|9|5[0|[2-8]|73)-?[1-9]\d{6}$/;
        if (!reg.test(phone)) {
            msgBox.innerHTML = "Phone number is illegal";
            return false;
        }
        msgBox.innerHTML = "";
        return true;
    }


function emailConfirm() {
    var email = document.getElementById("email").value;
    var msgBox = document.getElementById("emailMsg");
    var reg = /^\w+/;
    if (!reg.text(email)) {
        msgBox.innerHTML = "Mail can hava only one following letter";
        return false;
    }
    msgBox.innerHTML = "";
    reg = /^\w+([\.-]?\w+)*@\w+/;
    if (!reg.test(email)) { 
        msgBox.innerHTML = "Mail must have @";
        return false;
}

        reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
        if (!reg.test(email)) {
            msgBox.innerHTML = "invalid email";
            return false;
        }
        msgBox.innerHTML = "";
        return true;
    }

function isLetter(ch) {
        if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z"))
            return true;
        return false;
}

function isDigit(ch) {

    if (ch >= "0" && ch <= "9")
        true;
    false;

}
function English(str) {
    i = 0;
    while (str[i].isLetter) {
        i++;
    }
    if (i == str.length())
        return true;
    return false;

}


Solution 1:[1]

We need more information about exactly what happens in your success and failure cases. However I see potential issues here:

For me, this function does not work for two reasons:

function English(str) {
    i = 0;
    while (str[i].isLetter) {
        i++;
    }
    if (i == str.length())
        return true;
    return false;   
}

First, the variable i is not declared, do you mean this:

 let i = 0

Possibly, i is declared globally, and so you are inadvertently trashing another value? Generally using let is preferable to using var, you can have other unexpected effects on globals if you use var.

Second, I don't see how this is working. For me str[i].isLetter is not defined.

while (str[i].isLetter) {

Do you intend to use your isLetter() function

isLetter(str[i])

If that doesn't help you will need to explain in more detail what happens in your failure cases.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 djna