You need to move the return true;
to outside the for
loop. Otherwise your code will accept any string that simply starts with a Hebrew character.
function justHeb(str){
var ok = true;
for (var i=0;i<str.length&&ok;i++)
{
if (!str[i].match(/[א-ת]/))
{
ok=false;
return false;
}
}
return true;
}
Now, you could simplify even further by letting the regular expression engine do that work for you:
function justHeb(str) {
return /^[א-ת]*$/.test(str);
}
The leading ^
means that the match must start at the beginning of the string. The *
says to accept a sequence of zero or more characters in that range, and finally the $
says that the match must end at the end of the string.