I think what you want to do here is this:
var isOk = /[A-Z][a-zA-Z][^#&<>\"~;$^%{}?]{1,20}$/;
var nome = prompt("Digite o nome:");
(function testar(){
if(!nome) { //se não for digitado nada...
console.log("coloque o nome ")
}else if (!isOk.test(nome)) { //se o nome NÃO bater com o regex...
console.log("nome errado amiguinho");
} else { //acertou!
console.log("ok vá em frente");
}
})();
At line if(!nome)
, if the person does not enter any value, the value ""
is considered false
. The !
will invert and make the same true
, and run the code, asking you to type something. If any value is filled in, the filled string will count as true
, and when inverted it will false
, jumping to else if
.
In the else if (!isOk.test(nome))
part, test
will return true
if the name entered is validated by the regex, and then inverted by the exclamation. That is, if the name is NOT validated by the regex, erroneous answer. The last else
will respond unlike the previous if, ie if the name is correct.
There are better ways to do this, but I've edited your code as little as possible to make it functional.