javascript code to check special characters

后端 未结 6 1791
后悔当初
后悔当初 2020-12-08 10:45

I have JavaScript code to check if special characters are in a string. The code works fine in Firefox, but not in Chrome. In Chrome, even if the string does not contain spec

6条回答
  •  醉酒成梦
    2020-12-08 11:15

    Did you write return true somewhere? You should have written it, otherwise function returns nothing and program may think that it's false, too.

    function isValid(str) {
        var iChars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?";
    
        for (var i = 0; i < str.length; i++) {
           if (iChars.indexOf(str.charAt(i)) != -1) {
               alert ("File name has special characters ~`!#$%^&*+=-[]\\\';,/{}|\":<>? \nThese are not allowed\n");
               return false;
           }
        }
        return true;
    }
    

    I tried this in my chrome console and it worked well.

提交回复
热议问题