Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript检查字符串是否包含特殊字符_Javascript - Fatal编程技术网

使用Javascript检查字符串是否包含特殊字符

使用Javascript检查字符串是否包含特殊字符,javascript,Javascript,我有一个密码字段,我需要使用javascript检查它是否具有以下字符: !@$%^&* 我试着这样做,它的工作如预期: function ValidarPass() { var Contrasena = document.getElementById('Clave').value; if(Contrasena!='' && (Contrasena.indexOf('!')>-1|| Contrasena.indexOf('@'

我有一个密码字段,我需要使用javascript检查它是否具有以下字符:

!@$%^&*

我试着这样做,它的工作如预期:

function ValidarPass()
{
    var Contrasena = document.getElementById('Clave').value;

    if(Contrasena!='' && 
      (Contrasena.indexOf('!')>-1||
       Contrasena.indexOf('@')>-1||
       Contrasena.indexOf('#')>-1||
       Contrasena.indexOf('$')>-1||
       Contrasena.indexOf('%')>-1||
       Contrasena.indexOf('^')>-1||
       Contrasena.indexOf('&')>-1||
       Contrasena.indexOf('*')>-1))
    {
        alert("Exito!");
    }
    else
    {
        alert("Error!");
    }
}

是否有一种更简单/有效的方法来执行此操作?

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

函数有效(str){
return!/[!@$%^&*+=\-\[\]\',/{}\\\\\\\“:\?]/g.test(str);
}

实现这一点所用的正则表达式是[!@#$%^&*]

这将检查您列出的所有特殊字符。要在函数中使用,您可以使用matches函数,如下所示:

公共布尔isSpecialCharacter(字符串){
字符串模式=“[!@$%^&*]”;
返回string.matches(模式);
}

正则表达式是一个很好的单行解决方案。但它实际上比indexOf慢(就像您发布的代码)。请参阅此处的一些信息:

正则表达式文档:

函数有效(str){
return/[!@$%^&*(),.?“:{}|]/g.test(str);
}

另外,这个站点非常适合测试RegEx,虽然这里的两个答案都解决了这个问题,但为了演示,我将提交一个不使用RegEx的方法。也许我们并不总是需要使用正则表达式,即使是最简单的检查。这里的字符列表非常短,另一个选项可能是:

let testString = 'abcd';// the string you are checking
let chars = '!@#$%^&*'.split('');// just a way to write your chars as an array
let isValid = chars.some((c) => testString.includes(c));// check if at least one of the chars is included in your string
没有真正进行基准测试,但我希望它能很快完成,因为
some
方法应该在找到正匹配项时尽早返回,并且
也包括


可能您需要为
一些
包含
方法使用一些polyfill,或者根据您希望支持的浏览器类型,使用常规函数替换箭头函数,这只是为了演示一个想法。

我投票关闭这个问题,因为它要求改进工作代码。询问可能的副本,允许使用比要求更多的特殊字符。
function isValid(str){
 return /[!@#$%^&*(),.?":{}|<>]/g.test(str);
}
let testString = 'abcd';// the string you are checking
let chars = '!@#$%^&*'.split('');// just a way to write your chars as an array
let isValid = chars.some((c) => testString.includes(c));// check if at least one of the chars is included in your string