简体   繁体   English

用于检查特殊字符的JavaScript代码

[英]javascript code to check special characters

I have JavaScript code to check if special characters are in a string. 我有JavaScript代码来检查特殊字符是否在字符串中。 The code works fine in Firefox, but not in Chrome. 该代码在Firefox中运行良好,但在Chrome中无效。 In Chrome, even if the string does not contain special characters, it says it contains special characters. 在Chrome中,即使字符串不包含特殊字符,也表示它包含特殊字符。

var iChars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?";

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

Suppose I want to upload a file desktop.zip from any Linux/Windows machine. 假设我想从任何Linux / Windows机器上传一个文件desktop.zip The value of chkfile.value is desktop.zip in Firefox, but in Chrome the value of chkfile.value is c://fakepath/desktop.zip . chkfile.value的值是Firefox中的desktop.zip ,但在Chrome中, chkfile.value的值是c://fakepath/desktop.zip How do I get rid of c://fakepath/ from chkfile.value ? 如何从chkfile.value删除c://fakepath/

You can test a string using this regular expression : 您可以使用此正则表达式 测试字符串:

function isValid(str){
 return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}

Directly from the w3schools website: 直接来自w3schools网站:

   var str = "The best things in life are free";
   var patt = new RegExp("e");
   var res = patt.test(str);

To combine their example with a regular expression, you could do the following: 要将他们的示例与正则表达式结合起来,您可以执行以下操作:

function checkUserName() {
    var username = document.getElementsByName("username").value;
    var pattern = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/); //unacceptable chars
    if (pattern.test(username)) {
        alert("Please only use standard alphanumerics");
        return false;
    }
    return true; //good user input
}

Did you write return true somewhere? 你在某处写过return true吗? 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. 我在我的chrome控制台上试过这个,效果很好。

Try This one. 试试这个。

 function containsSpecialCharacters(str){ var regex = /[ !@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?]/g; return regex.test(str); } 

If you don't want to include any special character, then try this much simple way for checking special characters using RegExp \\W Metacharacter. 如果您不想包含任何特殊字符,请尝试使用RegExp \\ W Metacharacter检查特殊字符的这种简单方法。

var iChars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?";
if(!(iChars.match(/\W/g)) == "") {
    alert ("File name has special characters ~`!#$%^&*+=-[]\\\';,/{}|\":<>? \nThese are not allowed\n");
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM