用java代码怎么验证字符串是字母加数字或者字符组成的呢?

我在写注册系统,新手,纯自学的,求大神支招

可以用正则表达式来检验
比如这样就可以满足您全部的需求:
String reg="^[a-zA-Z0-9]{6,16}$";
String pwd="1234567890abcd";
boolean f=pwd.matches(reg);
System.out.println(f);

也就是密码必须6到16位,并且必须是a-z或者A-Z或者数字,才能匹配
匹配成功返回true,匹配失败返回false追问

正则表达式怎么验证邮箱的呢?求解...谢啦

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-08
做一个循环来判断字符串里的字符
bool includeNum = false;//包含数字

bool includeLetter = false;//包含字母
bool includeOther = false;//包含其它字符
for(int i=0; i < password.length; i++){
Char c = password.charAt(i);
if ( c >='0' && c <= '9' ){
includeNum = true;
} else if ( (c >= 'a' && c<='z') || ( c >='A'&&c<='Z') ){
includeLetter = true;
} else {
includeOther = true;
}
}本回答被提问者采纳
相似回答