js java,验证字符串是否为数字

java的

public boolean isNum(String str)

{

return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");

}

 //如果传入空的字符串,进行验证,返回会true

public class NumberDemo {
 public static void main(String[] args) {
  String str1="1122.2.2";
  String str2="111";
  String str3="111.2";
  String str4="111s";
  String str5="111.s";
  String str6="1s11";
  System.out.println(str1+":"+isNum(str1));
  System.out.println(str2+":"+isNum(str2));
  System.out.println(str3+":"+isNum(str3));
  System.out.println(str4+":"+isNum(str4));
  System.out.println(str5+":"+isNum(str5));
  System.out.println(str6+":"+isNum(str6));
 }
 public static boolean isNum(String str){
  return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
 }
}
结果:
1122.2.2:false
111:true
111.2:true
111s:false
111.s:false
1s11:false

//-----------------------------------------------------------------

js的:

var inventory_location_id_org=$('#inventory_location_id').val();
                if(isNaN(inventory_location_id_org)){
                 showAlert("请选择货位,或不选择");
                 return false;
                }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值