Javascript 用于检查特殊字符的javascript代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11896599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:35:34  来源:igfitidea点击:

javascript code to check special characters

javascript

提问by ankit

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 special characters, it says it contains special characters.

我有 JavaScript 代码来检查字符串中是否包含特殊字符。该代码在 Firefox 中运行良好,但在 Chrome 中不起作用。在 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.zipfrom any Linux/Windows machine. The value of chkfile.valueis desktop.zipin Firefox, but in Chrome the value of chkfile.valueis c://fakepath/desktop.zip. How do I get rid of c://fakepath/from chkfile.value?

假设我想desktop.zip从任何 Linux/Windows 机器上传文件。的价值chkfile.valuedesktop.zip在Firefox,但在Chrome的价值chkfile.value就是c://fakepath/desktop.zip。我该如何摆脱c://fakepath/chkfile.value

回答by KooiInc

You can testa string using this regular expression:

您可以使用此正则表达式测试字符串:

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

回答by Koffy

Directly from the w3schoolswebsite:

直接从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
}

回答by sedran

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

你有没有写什么return true地方?你应该写它,否则函数什么也不返回,程序也可能认为它是假的。

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 控制台中试过这个,效果很好。

回答by Vishal Patidar

Try This one.

试试这个。

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

回答by Chaitanya

If you don't want to include any special character, then try this much simple way for checking special characters using RegExp \WMetacharacter.

如果您不想包含任何特殊字符,请尝试使用 RegExp \WMetacharacter这种非常简单的方法来检查特殊字符。

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

回答by Dylan Barber

You could also do it this way.

你也可以这样做。

specialRegex = /[^A-Z a-z0-9]/ specialRegex.test('test!') // evaluates to true Because if its not a capital letter, lowercase letter, number, or space, it could only be a special character

specialRegex = /[^A-Z a-z0-9]/ specialRegex.test('test!') // evaluates to true 因为如果它不是大写字母、小写字母、数字或空格,则只能是特殊字符