Java 验证前台返回的是不是百分数 在后台用正则表达式验证百分比数据

public static void main(String[] args) {

String returnStr="10%";
String reg = "^([0-9.]+)[ ]*%$";     
if(returnStr.matches(reg)){
System.out.println("是");
}else{
System.out.println("不是");
}
}

 后台写法:

String reg = "^([0-9.]+)[ ]*%$";      //注意编码下的% ( %% )有可能不一致  把页面返回来的%复制在"^([0-9.]+)[ ]*%$"就可以了

String test= (String) params.get("test");

if(!test.matches(reg)){  //test代表返回的百分比参数

return"百分比格式错误!";
}

//如果返回来的是多个百分比就循环
String[] str = test.split("\\,");   
for(int z=0;z<str.length;z++){
if(!str[z].matches(reg)){
return"百分比格式错误!";
}
}

 

 

posted on 2019-10-10 16:34  多言  阅读(2248)  评论(0编辑  收藏  举报

导航