JS实现:
<script language="javascript">
var str = "132651";
if( str.match(/^[0-9]+$/))
alert("数字");
</script>
JAVA实现:
public class Test{
public final static String REG_DIGIT="[0-9]*";
public final static String REG_CHAR="[a-zA-Z]*";
public Test(){}
public boolean isDigit(String str){
return str.matches(REG_DIGIT);
}
public boolean isChar(String str){
return str.matches(REG_CHAR);
}
public static void main(String args[]){
Test t=new Test();
String s1="dsssdcd";
String s2="4014810";
String s3="jflj998";
System.out.println("s1 is a non-digit string:"+t.isChar(s1));
System.out.println("s1 is a digit string:"+t.isDigit(s1));
System.out.println("s2 is a non-digit string:"+t.isChar(s2));
System.out.println("s2 is a digit string:"+t.isDigit(s2));
System.out.println("s3 is a non-digit string:"+t.isChar(s3));
System.out.println("s3 is a digit string:"+t.isDigit(s3));
}
输出结果
s1 is a non-digit string:true
s1 is a digit string:false
s2 is a non-digit string:false
s2 is a digit string:true
s3 is a non-digit string:false
s3 is a digit string:false