包含至少1个数字和1个字符且固定长度为11的字母数字字符串的正则表达式 [英] Regex for alphanumeric string with at least 1 number and 1 character and a fixed length of 11
本文介绍了包含至少1个数字和1个字符且固定长度为11的字母数字字符串的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要正则表达式来匹配长度为11个字符的字母数字字符串,但该值必须至少包含1个字符和1个数字.
I need regex to match an alphanumeric string exact 11 characters long but the value must contain at least both 1 character and 1 number.
即/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
即^[a-zA-Z0-9]{6,}$
通过使用像这样的OR(||)运算符
by using OR (||) operator like this
//regex code
var str = "";
if ($.trim($("input[id$='txtBranchName']").val()) != "")
str = $.trim($("input[id$='txtBranchName']").val());
var reg_exp = /^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i; // /^[a-zA-Z0-9]{11,}$/;//^(\d)(?:\1+)?$/; // new RegExp('([0-9]){6}');
var reg_exp2 = /^[a-zA-Z0-9]{11,11}$/;
if (!reg_exp.test(str) || !reg_exp2.test(str)) {
$("span[id$='lblError']").css("color", "red");
$("span[id$='lblError']").html($("span[id$='lbl_PayeeInformation_IFSCNo']").html()).show();
$("input[id$='txtBranchName']").focus();
returned = false;
return false;
}
//end regex code
但是如果我用一个正则表达式得到它,那就太好了.
but it would be great if i get it in one regex.
推荐答案
您需要将两者结合起来并使用 {11}
精确匹配11个字符.
You need to combine both and use {11}
for exact 11 characters match.
/^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{11}$/
位置:
Where :
-
(?=.*\d)
断言该位置在任何位置(其中\d
等同于[0-9]
)都在数字后面. -
(?=.*[a-zA-Z])
断言该位置在任何位置都跟随字母. -
[a-zA-Z0-9]{11}
仅在长度为11个字符且在字符类内时匹配. -
^
和$
是开始和结束锚点,有助于检查整个字符串.
(?=.*\d)
assert that the position follows a digit at any position(where\d
is equivalent to[0-9]
).(?=.*[a-zA-Z])
assert that the position follows an alphabet at any position.[a-zA-Z0-9]{11}
matches only when the length is 11 characters and within the character class.^
and$
are start and end anchors which help to check whole string.
这篇关于包含至少1个数字和1个字符且固定长度为11的字母数字字符串的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文