Home > Blockchain >  Do I have a problem with my javascript or is it something else?
Do I have a problem with my javascript or is it something else?

Time:04-24

(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;

}

CodePudding user response:

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.

CodePudding user response:

Try this:

const reg = /[0-9]/;
const reg1 = /\w/;
const reg2 = /\s/;
let msgBox = document.getElementById('NameMsg');

function validateform() {
  msgBox.innerHTML = '';
  try {
    userNameVal();
    passowrdVal();
    ConfirmPhone();
    emailConfirm();
    lastName();
    city();
    Name();
    return true;
  } catch (err) {
    msgBox.innerHTML = err;
  }
}

function Name() {
  const firstName = document.getElementById('firstName').value;
  if (firstName.length == 0) {
    throw new Error('You must enter your name');
  }
  if (
    reg.test(firstName) &&
    reg1.test(firstName) &&
    reg2.test(firstName) &&
    English(firstName)
  ) {
    throw new Error("Your name can't have a number, space or a special char");
  }
}
function lastName() {
  const LastName = document.getElementById('LastName').value;
  if (Name.length == 0) {
    throw new Error('You must enter your name');
  }
  if (reg.test(LastName) || reg1.test(LastName) || reg2.test(LastName)) {
    throw new Error("Your name can't have a number, space or a special char");
  }
}
function city() {
  const CityName = document.getElementById('CityName').value;
  if (CityName.length == 0) {
    throw new Error('You must enter your City');
  }
  if (reg.test(CityName) || reg1.test(CityName) || reg2.test(CityName)) {
    throw new Error("Your name can't have a number, space or a special char");
  }
}

function userNameVal() {
  const userName = document.getElementById('userName').value;
  if (userName.length == 0) {
    throw new Error('You must enter a username');
  }
  if (!isLetter(userName[0])) {
    throw new Error('Your username must start with a letter');
  }
}

function passowrdVal() {
  const pass = document.getElementById('password').value;
  const specialChar = /[@!#$%^&*()- ]/;
  if (pass.length == 0) {
    throw new Error('You must enter a password');
  }
  if (pass.length < 7) {
    throw new Error('The password must contain at least 7 charactes');
  }
  if (!specialChar.test(pass)) {
    throw new Error('password must contain one special letter');
  }
}

function ConfirmPhone() {
  const phone = document.getElementById('phone').value;
  const regx = /^0{1}(2|3|4|6|8|9|5[0|[2-8]|73)-?[1-9]\d{6}$/;
  if (!regx.test(phone)) {
    throw new Error('Phone number is illegal');
  }
}

function emailConfirm() {
  const email = document.getElementById('email').value;
  let regx = /^\w /;
  if (!regx.text(email)) {
    throw new Error('Mail can hava only one following letter');
  }
  regx = /^\w ([\.-]?\w )*@\w /;
  if (!regx.test(email)) {
    throw new Error('Mail must have @');
  }
  regx = /^\w ([\.-]?\w )*@\w ([\.-]?\w )*(\.\w{2,4}) $/;
  if (!regx.test(email)) {
    throw new Error('invalid email');
  }
}

function isLetter(ch) {
  if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return;
  throw new Error();
}

function isDigit(ch) {
  if (ch >= '0' && ch <= '9') return;
  throw new Error();
}
function English(str) {
  let i = 0;
  while (isLetter(str[i])) {
    i  ;
  }
  if (i == str.length()) return;
  throw new Error();
}
  • Related