简体   繁体   English

javascript 至少一个数字和一个大写字母的正则表达式

[英]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.

相关问题 至少一位数字的字母数字正则表达式 - Regular expression for alphanumeric with atleast one digit 正则表达式,至少一个字符和一个数字,重复不超过两个 - Regular Expression for atleast one character and one number with no repeat more than two 正则表达式强制在字符串中至少包含一个字母和数字? - regular expression to force to have at least one letter and number in a string? 正则表达式:允许字母,数字和空格(至少有一个字母不是数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number) 正则表达式:允许字母,数字和空格(至少包含一个字母或数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number) 如何使用至少一个数字,一个大写字母和6-20个字符验证密码? - How to validate password with atleast one numeric, one Uppercase letter and 6-20 characters? JavaScript正则表达式匹配数字后跟字母 - JavaScript Regular Expression to match a number followed by a letter 正则表达式至少匹配一个大写字母和至少一个数字和任意数量的特殊字符 - Regular expression to match at least One capital letter and at least One digit and any number of special charecter 至少一个数的正则表达式 - Regular Expression For At Least One Number 3个大写字母的正则表达式,不多或少 - Regular Expression for 3 uppercase letter, no more or less
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM