0

I wanna put the check (if) before user can type his name, but it doesn't work. I can still put numbers in the input. What's wrong?

var name1 = prompt('enter name');
var surname = prompt('enter surname');
var patronymic = prompt('enter secondname');
var fullName = name1 + " " + surname + " " + patronymic;


if (typeof(name1) === "number" || typeof(surname) === "number"|| typeof(patronymic) === "number") {
    do {
        alert('wrong, try again');
        name1 = prompt('enter name');
        surname = prompt('enter surname');
        patronymic = prompt('enter ');
    }
    while (typeof(name1) === "string" && typeof(surname) === "string" && typeof(patronymic) === "string");
}

alert("U " + fullName);
gunniq
  • 21
  • 5

3 Answers3

1

typeof(name1) === "number" would always be false as name1 as prompt input string would always be string, if not empty (check propmt document for its return type). Please check this fix, we can use regex regular expression to check whether input is a number.

Please check the isNumber function and the comments.

var name1 = prompt('enter name');
var surname = prompt('enter surname');
var patronymic = prompt('enter secondname');


// a function to check whether the input string is number
function isNumber(str) {
  return !!( // !! symbol is to cast a variable to be a boolean (true or false) value
    str && // check whether str is null, if it is then we'll return false, as null is not a number
    str.match(/^\d+$/)); // use regular expression to check whether the input only contains digits. Please check the reference posts in my answer for more information about regex
}

if (isNumber(name1) || isNumber(surname) || isNumber(patronymic)) {
  do {
    alert('wrong, try again');
    name1 = prompt('enter name');
    surname = prompt('enter surname');
    patronymic = prompt('enter ');
  }
  while (isNumber(name1) || isNumber(surname) || isNumber(patronymic));
}
var fullName = name1 + " " + surname + " " + patronymic;

alert("U " + fullName);

More Reference

Mark
  • 999
  • 1
  • 7
  • 15
  • thanks, that works. But i have no idea what (--return !!(str && str.match(/^\d+$/));--) does tho :) I just started learning JS today – gunniq Jan 16 '21 at 09:11
  • @gunniq I added comments in `isNumber` function, please take a look. Feel free to drop a message if you have more questions. – Mark Jan 16 '21 at 09:15
  • @gunniq regular expression is somehow not for first day learners, so if you feel the function is too hard you totally can skip it for now, it won't be a blocker to your JS learning journey ;) I'll suggest you to just take `isNumber` function as a black box for now – Mark Jan 16 '21 at 09:18
  • last question, can i replace my if's with questionmark anywhre in my code? – gunniq Jan 16 '21 at 09:33
0

Prompt always return a string. If you entered a number it is also return to the variable as a string

You can use the following method to check for numbers in a string.

var name = prompt('Enter');
isNaN(Number(name)) //this will return false if name is a number
0

Use Regular Expression to check numbers.

var name1;
var surname;
var patronymic;
name1= prompt('enter name');
while(/\d/.test(name1)){
    alert('wrong, try again');
    name1= prompt('enter name');
}
surname = prompt('enter surname');
while(/\d/.test(surname)){
    alert('wrong, try again');
    surname= prompt('enter surname');
}
patronymic = prompt('enter secondname');
while(/\d/.test(patronymic)){
    alert('wrong, try again');
    patronymic= prompt('enter secondname');
}
var fullName = name1 + " " + surname + " " + patronymic;
alert("U " + fullName);