正则表达式在jQuery验证 - jQuery的单引号问题 [英] Regex in jQuery Validation - Single quote issue in jQuery

查看:303
本文介绍了正则表达式在jQuery验证 - jQuery的单引号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正规的前pression验证密码包含数字,小写和大写字符。

下一部作品的罚款。但是,当我在jQuery验证使用:

  txtPassword:{
    要求:真实,
    正则表达式:正则表达式
}正则表达式= (?=^.{8,16}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{":;/'?/>.<,])(?!.*\\s).*$

但问题是单引号 在Javascript。一些机构可以请帮我解决这个问题?三江源。


解决方案

有需要固定在这个正则表达式的几件事情:


  • 在前瞻检查密码,你有没有时间
    一个量词。因此,它总是会失败的检查。为例如,对于
    一些检查,你应该有(?=。* \\ D)而不是的(?= \\ D)

  • 您不要用斜杠 / 转义字符,而不是你
    使用反斜杠 \\ 。你必须将其与一个逃脱不正确
    forwardslash在最后一个字符类。

  • 字符内
  • 的唯一特殊字符或元字符
    类是右括号(] ),反斜线( \\ ),插入符号( ^
    和连字符( - )。因此它是不因单引号。罪魁祸首是你插入符号未逃脱。

试试这个正则表达式,其中插入符号 ^ 转义,而不是报价

<$p$p><$c$c>(?=^.{8,16}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%\\^&*()_+}{\":;'?/>.<,])(?!.*\\s)^.*$


我对你的问题假设,你的意思是一个正则表达式来验证一个密码:


  • 在至少1号

  • 在至少1 lowerchase字符

  • 在至少1个大写字符

  • 在至少1个特殊字符出集 @#$%^&放大器; *()_ +} {:;。'/&GT;&LT;

  • 长度与16之间的8个字符

如果这是真的,请与澄清的要求更新了你的问题。


在<一个正则表达式播放href=\"http://regexpal.com/?flags=g&regex=%28%3F%3D%5E.%7B8%2C16%7D%24%29%28%3F%3D.%2a%5Cd%29%28%3F%3D.%2a%5Ba-z%5D%29%28%3F%3D.%2a%5BA-Z%5D%29%28%3F%3D.%2a%5B!%40%23%24%25%5C%5E%26%2a%28%29_%2B%7D%7B%22%3A%3B%27%3F%2F%3E.%3C%2C%5D%29%28%3F!.%2a%5Cs%29%5E.%2a%24&input=P%40sSw0rD\"相对=nofollow> RegexPal


基于jQuery的code。捣鼓这个这里

  VAR正则表达式= /(?=^.{8,16}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%\\^&*()_+}{\":;'?/>.<,])(?!.*\\s)^.*$/g;
//if(regex.test($('#txtPassword)VAL())){警报('匹配'); }其他{警报('不匹配')}
如果(regex.test('P @ sSw0rD')){
    警报('匹配');
}其他{
    警报('不匹配');
}

I have a regular expression to validate a password to contain numbers, lowercase and uppercase characters.

The one below works fine. But when I use this in jQuery Validation:

txtPassword: {
    required: true,
    regex: Regex
}

Regex = (?=^.{8,16}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&amp;*()_+}{&quot;:;/'?/&gt;.&lt;,])(?!.*\s).*$

But the issue is single quote ' in Javascript. Can some body please help me to resolve this? Thankyou.

解决方案

There are several things that need to fixed in this regex:

  • In your lookahead checks for the password, you have a period without a quantifier. Hence it will always fail that check. For e.g., for the number check, you should have (?=.*\d) instead of (?=.\d).
  • You do not escape characters with a forward-slash /, instead you use a backslash \. You have it escaped incorrectly with a forwardslash in the last character class.
  • The only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-). Hence it is not due to the single quote. The culprit is your caret which is not escaped.

Try this regex where the caret ^ is escaped instead of the quote ':

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


I am assuming by your question, you meant a Regexp to validate a password with:

  • at least 1 number
  • at least 1 lowerchase character
  • at least 1 uppercase character
  • at least 1 special character out of the set !@#$%^&*()_+}{":;'?/>.<,
  • length between 8 and 16 characters

If this is true, please update your question with the clarified requirement.


Play with the regex in RegexPal


jQuery based code. Fiddle with this here:

var regex = /(?=^.{8,16}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%\^&*()_+}{":;'?/>.<,])(?!.*\s)^.*$/g;
//if(regex.test($('#txtPassword').val())){ alert('Match'); }else{alert('No Match')}
if (regex.test('P@sSw0rD')) {
    alert('Match');
} else {
    alert('No Match');
}​

这篇关于正则表达式在jQuery验证 - jQuery的单引号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆