如何使输入密码至少需要一个特殊字符(包括括号)

我想知道如何将带有括号的特殊字符部分(如 {} [] () 和其他内容(如“')添加到我在下面尝试的唯一字符中,但由于某种原因,当我添加另一个字符时它停止工作.


<-- works but does not have any brackets or quotes for special character-->

<form acton = "#">      

    <input type="password" id="newPass" required

      pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[!@#$%^&*+`~=?\|<>/]).{8,}"


    <button>submit</button>


</form>

上面的部分有效,但没有引号或更多特殊字符


下面的代码有括号但不起作用


<-- does not work (if you do not enter special character user will be able to submit but it does not have any brackets -->


<form acton = "#">      

    <input type="password" id="newPass" required

      pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[~`!@#$%^&*()-_=+[]{};:'.,"\|/?><]).{8,}"


    <button>submit</button>


</form>

这也不起作用(来自答案)


<form acton = "#">      

    <input type="password" id="pass" required

      pattern="(?=.*\d)(?=.*[a-z])(?=.*?[0-9])(?=.*?[~`!@#$%\^&*()\-_=+\[\]{};:\'.,\"\\|/?\>\<]).{4,}">


    <button>submit</button>


</form>

我正在寻找一个使底部(带括号的那个和我包括的所有特殊字符)有效的答案。


jeck猫
浏览 116回答 3
3回答

森栏

因为它是正则表达式,所以里面的所有字符[.....]都是允许的,所以你应该在那里加上括号。您可以使用转义字符来做到这一点(因为括号在正则表达式中起作用)。所以只需将\]and添加\[到里面允许的字符[.....]。尝试:<input type="password" name="pw" pattern="(?=.*?[#?!@$%^&*-\]\[])"顺便说一句.. 我建议使用正则表达式备忘单,它将在验证任务中帮助您很多。至于你的编辑:和需要逃脱"。'虽然\" \'不起作用,但您可以使用它们\x27,它们是ascii 表中的和\x22的十六进制表示。"'尝试:<form acton = "#">&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <input type="password" id="pass"&nbsp; required pattern = "(?=.*\d)(?=.*&nbsp;&nbsp; &nbsp; &nbsp;[a-z])(?=.*?[0-9])(?=.*?[~`!@#$%\^&*()\-_=+[\]{};:\x27.,\x22\\|/?><]).{4,}">&nbsp; &nbsp; &nbsp; &nbsp; <button>submit</button></form>

慕妹3146593

\]如果在模式搜索期间需要匹配正则表达式(例如 )字符,您应该转义它们。另外,请参阅现有答案:密码中的大括号和方括号以下对我有用,希望它对你有用:(我只是添加了转义字符)&nbsp;&nbsp;&nbsp;<input&nbsp;type="password"&nbsp;id="newPass"&nbsp;required &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[!@#$%^&*+`~'=?\|\]\[\(\)\-<>/]).{8,}">

MYYA

你实际上非常接近。最大的错误是:你应该escape在里面的一些特殊字符character group并且你应该有hyphen as the last character(否则它是一个范围)。这是您要求的 RegExp:<input&nbsp;type="password"&nbsp;id="pass"&nbsp;&nbsp;required&nbsp;pattern&nbsp;=&nbsp;"(?=.*\d)(?=.*[a-z])(?=.*?[0-9])(?=.*?[~`!@#$%^&amp;*()_=+\[\]{};:&apos;.,&quot;\\|\/?&gt;&lt;-]).{4,}">更新:我忘了对模式进行 html 编码以直接在 html 中使用。现在它应该可以工作了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript