1.数字(不是数值,可以是0开头的:01234567)
public boolean isNumber(String str){
java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("[0-9]*");
java.util.regex.Matcher match=pattern.matcher(str);
if(match.matches()==false){
return false;
}else{
return true;
}
}
2.0和正整数
public boolean isInteger(String str){
java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("^(0|[1-9][0-9]*)$");
java.util.regex.Matcher match=pattern.matcher(str);
if(match.matches()==false){
return false;
}else{
return true;
}
}
3.两位小数的浮点数(注意:java转义要用\\,js一个\就行了)
public boolean isFloat2(String str){
java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("^[0-9]+(\\.[0-9]{1,2})?$");
java.util.regex.Matcher match=pattern.matcher(str);
if(match.matches()==false){
return false;
}else{
return true;
}
}
4.电话号码(7,8,11位数字)
public boolean checkTel(String str){
java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("^1[0-9]{10}$|^[0-9]{7,8}$");
java.util.regex.Matcher match=pattern.matcher(str);
if(match.matches()==false){
return false;
}else{
return true;
}
}
Author:立礼
Sign:人生不要有太多的幻想,而要有更多的行动