我想检查JavaScript中的字符串是否严格为字母拼写。我已经试过了:
var Exp = /^[0-9a-z]+$/;
if(!pwd.match(Exp))
alert("ERROR")
但是问题在于,如果它包含所有字母或所有数字,它会通过输入字符串,如果要同时包含字符和数字,我希望函数通过。
尝试此正则表达式:
/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+[0-9a-z]+$/i
仅允许使用字母数字。
不允许:
正则表达式下方允许:
/^([0-9]|[a-z])+([0-9a-z]+)$/i
您可以使用lookaheads:
/^(?=.*?[a-z])(?=.*?\d)[a-z\d]+$/i
^^^^^^^^^^ ^^^^^^^
at least at least
one letter one digit
FYI,限制密码允许使用的字符会减少熵很多(现在只有36个不同的字符),因此使其更容易破解。不要做这个限制。不过,检查字符串是否包含某种类型的字符是很好的(嗯,有一些理论也可以减少熵,但是我对此没有足够的了解)。
另请参见:Regex to accept atleast one alphabet one numeric char and one special Character
Felix Kling的答案是我认为最好的单正则表达式解决方案,但我会考虑采用三种模式来实现。这使您可以返回有用的错误消息:
if(pwd.match(/[^0-9a-z]/i))
alert("Only letters and digits allowed!");
else if(!pwd.match(/\d/))
alert("At least one digit required!");
else if(!pwd.match(/[a-z]/i))
alert("At least one letter required!");
else
// process pwd
尽管您应该考虑将非字母数字字符用作密码,但这会使它们更加安全。
var pwd = 'somevalue';
if(pwd.match("^[a-zA-Z0-9]*$")){
alert('aplanumeric');
}
else{
alert('Not alphanumeric');
}
String myString1 =“ abcdefghijklmnop1”;
/**
* To Check Alphanumeric myString1==ABCD is Working;myString1==123 is Working;myString1==ABCD123 is not Working
*/
Pattern p1 = Pattern.compile("^[a-zA-Z0-9]*$");
如果您检查p1是否同时包含字母和数字,则p1将显示为false,因此,如果要检查其是否包含字母或数字,则可以使用模式p1,请使用模式p
/**
* To Check both Alpha and Numeric only myString1==ABCD123 is Working
*/
Pattern p = Pattern
.compile("[a-zA-Z]+[0-9]+[a-zA-Z0-9]*|[0-9]+[a-zA-Z][a-zA-Z0-9]*");
boolean hasSpecialChar = p.matcher(myString1).find();
if (hasSpecialChar) {
System.out.println("Working");
} else {
System.out.println("not Working");
}
/***************OR*******************/
char [] charArray = myString1.toCharArray();
for (char ch : charArray) {
if (Character.isUpperCase(ch)) {
}
if (Character.isLetter(ch)) {
containsletter = true;
}
if (Character.isDigit(ch)) {
containsdigits = true;
}
}
if(containsletter&&containsdigits){
System.out.println("Working");
}
else{
System.out.println("not Working");
}
}
正确的功能是
function isAlphaNumeric(str) {
return str.match(/^[a-z0-9]+$/i) !== null;
}
没有正则表达式的一种非常简单的方法:
function stringIsStrictlyAlphaNumeric(string)
{
//returns true for letters, digits or both
var stringIsLooselyAlphanumeric = string => [...string].every(character => '0123456789abcdefghijklmnopqrstuvwxyzABCEDEFGHIJKLMNOPQRSTUVWXYZ'.includes(character));
//returns true for digits only
var stringIsComposedOfDigitsOnly = string => [...string].every(character => '0123456789'.includes(character));
//returns true for letters only
var stringIsComposedOfLettersOnly = string => [...string].every(character => 'abcdefghijklmnopqrstuvwxyzABCEDEFGHIJKLMNOPQRSTUVWXYZ'.includes(character));
//returns true for letters and digits only.
return stringIsLooselyAlphanumeric(string) && !stringIsComposedOfDigitsOnly(string) && !stringIsComposedOfLettersOnly(string);
}
请这样使用:
stringIsStrictlyAlphaNumeric('hello123'); //returns true
stringIsStrictlyAlphaNumeric('hello'); //returns false
stringIsStrictlyAlphaNumeric('123'); //returns false
stringIsStrictlyAlphaNumeric(''); //returns false