Codename One - RegexConstraint to check a valid phone number

人盡茶涼 提交于 2019-12-08 08:43:27

[rant] Personally I really hate regex as I find it damn unreadable for anything other than trivial validation. [/rant]

So I would prefer this:

val.addConstraint(phone, new Constraint() {
   public  boolean isValid(Object value) {
       String v = (String)value;
       for(int i = 0 ; i < v.length() ; i++) {
          char c = v.charAt(i);
          if(c >= '0' && c <= '9' || c == '+' || c == '-') {
              continue;
          }
          return false;
       }
       return true;
   }

   public String getDefaultFailMessage() {
       return "Must be valid phone number";
   }
});

However, I'm guessing the reason the regex failed for you is related to the syntax with the slashes:

String phoneRegEx = "^\\(?([0-9]{3})\\)?([ .-]?)([0-9]{3})\\2([0-9]{4})";
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!