jquery - javascript regular expression for atleast one number and one uppercase letter - Stack Overflow

what would be the regular expression to check if a given string contains atleast one number and one upp

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;

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;
Share Improve this question edited Apr 4, 2012 at 2:52 pri_dev asked Apr 4, 2012 at 2:31 pri_devpri_dev 11.7k16 gold badges75 silver badges122 bronze badges 3
  • like /[0-9]+[A-Z]+/ or /[0-9]+[A-Z]/? – mshsayem Commented Apr 4, 2012 at 2:34
  • 2 You need to provide more info, such as examples of what you want to match and what you want to not match. You've given nothing here that makes your question possible to answer. Please edit to improve it so people can help you. Thanks. :) – Ken White Commented Apr 4, 2012 at 2:42
  • the digit & uppercase can appear anywhere is the string – pri_dev Commented Apr 4, 2012 at 2:55
Add a ment  | 

3 Answers 3

Reset to default 3

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/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 (/^(?=.*[A-Z])(?=.*\d).*$/) will return the entire password if it matches the criteria.

('a3sdasFf').match(/^(?=.*[A-Z])(?=.*\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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744522978a4578683.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信