[英]javascript regular expression for atleast one number and one uppercase letter
what would be the regular expression to check if a given string contains atleast one number and one uppercase letter?检查给定字符串是否至少包含一个数字和一个大写字母的正则表达式是什么? Thanks in advance提前致谢
I am doing like this我这样做
function validate_pass{
var var_password = document.getElementById("npassword").value;
else if (!(/^(?=.*\d)(?=.*[A-Z]).+$/).test(var_password))){
msg = "Password should contain atleast.";
showErrorMsgPassword(msg);
$('#npassword').val('');
$('#cpassword').val('');
return false;
}
else return true;
If the desire is to test a string to see if it has a least one digit and at least one uppercase letter in any order and with any other characters allowed too, then this will work:如果想要测试一个字符串以查看它是否具有至少一位数字和至少一个大写字母(以任何顺序排列)并且允许任何其他字符,那么这将起作用:
var str = "abc32Qdef";
var re = /[A-Z].*\d|\d.*[A-Z]/;
var good = re.test(str);
Working demo with a bunch of test cases here: http://jsfiddle.net/jfriend00/gYEmC/这里有一堆测试用例的工作演示: http://jsfiddle.net/jfriend00/gYEmC/
It's been a while since I've done this, and I'm fairly certain there is a more efficient way than this.自从我这样做以来已经有一段时间了,而且我相当确定有比这更有效的方法。 You will need to use positive lookaheads, but there should be a way to remove the wildcards from within them:您将需要使用正面前瞻,但应该有一种方法可以从其中删除通配符:
This regex ( /^(?=.*[AZ])(?=.*\d).*$/
) will return the entire password if it matches the criteria.如果符合条件,此正则表达式 ( /^(?=.*[AZ])(?=.*\d).*$/
将返回整个密码。
('a3sdasFf').match(/^(?=.*[AZ])(?=.*\d).*$/);
Like below:像下面这样:
/(\d.*[A-Z])|([A-Z].*\d)/
To test a string matched or not:测试字符串是否匹配:
var str = "abc32dQef";
var is_matched = /(\d.*[A-Z])|([A-Z].*\d)/.test(str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.