indexOf() function to return a -1 value
javascript
Solution
You could use...
var value = field1.value;
for (var i = 0, length = value.length; i < length; i++) {
if (iChars.indexOf(value.charAt(i)) != -1) {
alert('Problem!');
break;
}
}
Problem
I have a script that I'm trying to get working. Basically, what I'm trying to do is prevent someone from entering in special characters in to a field. the function I have is as follows: ``` var iChars = "!@#$%^&*()+=[];,./{}|<>?;"; if (field1.value.indexOf(iChars) !=-1) { alert ("problem") } ``` The issue I'm having is that the field is searching for the exact match to the iChars var instead of matching any single value. For example if I create a var test ="one" and enter "one" into the field, it returns with the error, but if I enter "o" into the field, it does not return anything and just passes through to the next portion of the script - but if i entered "none" or "oneeee" it would push the error through. Any help on fixing this? I tried looking into arrays for indexOf but didn't really understand it, so if you are going to suggest it can you please explain in as much detail as possible. Thanks