Excuse me if the mistake is very silly, because I am just getting started with Javascript (and programming in general, by the way, I like it) I am trying to create a small program, and in one of the input fields, you have to write your name. I made a function to prevent that you include characters such as &%$?¿ and so on…
The problem is that the function works fine… sometimes yes, sometimes no… it seems to depend of where exactly you type the non valid characters, but due to my limited understanding of programming, I still cannot understand what am I doing wrong. Here, take a look:
function validate_input(text) {
var notgood = "!"·$%&/()=?¿@#¬"; //cannot have this inside name
var i = 0;
while (i <= notgood.length) {
if (text.indexOf(notgood.charAt(i)) ==-1) {
ind = ind + 1;
break;
}
else {
alert("Your name cannot include symbols");
var text = prompt("Try again");
}
}}
A million thanks. Is just a small detail. I can have my input without checking for symbols, but I feel a bit silly of not being able to understand this problem…
You code might work, if you escape the second
"in the notgood string:Although I see many other potential issues, like the
indglobal.Or you can try a regexp instead, it’s cleaner for your use case