1

In the following code, the RegexConstraint doesn't work, because the phone number results always incorrect. What's wrong? I need to check a mobile phone number (without the country code). For example, the input 3652312453 should be correct, but in the following code it's evaluated as incorrect. I copied the regex from the discussion linked in the comment: my only requirement is a valid phone number.

(Note: this question is not for generic Java, but only for Codename One. The class "CountryCodePicker" extends the class "Button": I reported it to make it clear that the phone number and the country code are separated)

    TextModeLayout tl = new TextModeLayout(1, 1);
    Container loginContainer = new Container(tl);
    TextComponent phone = new TextComponent().label("PHONE").errorMessage("INVALID-PHONE");
    CountryCodePicker countryCode = new CountryCodePicker();
    phone.getField().setConstraint(TextArea.PHONENUMBER);
    loginContainer.add(phone);
    Container loginContainerWithCodePicker = new Container(new BoxLayout(BoxLayout.X_AXIS_NO_GROW));
    loginContainerWithCodePicker.add(countryCode).add(loginContainer);
    // https://stackoverflow.com/questions/8634139/phone-validation-regex
    String phoneRegEx = "/\\(?([0-9]{3})\\)?([ .-]?)([0-9]{3})\\2([0-9]{4})/";
    val.addConstraint(phone, new RegexConstraint(phoneRegEx, "NOT-VALID-NUMBER"));
    Button loginButton = new Button("LOG-IN");
    val.addSubmitButtons(loginButton);
Francesco Galgani
  • 6,137
  • 3
  • 20
  • 23

2 Answers2

1

[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})";
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Thank you, the first approch is a lot more readable and customizable. Maybe this opportunity should be reported in the API. – Francesco Galgani Jan 29 '18 at 15:22
  • I tried to replace "iter++" with "i++" in the for loop, but Stack Overflow doesn't allow me this edit in your answer. – Francesco Galgani Jan 29 '18 at 15:25
  • Thanks, faulty brain compiler. If you think the docs can be improved you can literally just click edit and help us clarify them https://www.codenameone.com/blog/tip-edit-docs-fun-profit.html You don't need to worry about making mistakes etc. because we review all edits and make sure they are correct. – Shai Almog Jan 30 '18 at 05:07
1

The following resource on Oracle's Java 8 Lessons helps me always define the RegEx I need. Take the time to study it and you will be successful, especially since the problem seems to be in the initialization of the Constraint object. I had an issue the day before my post here, and managed to solve it elegantly which is the goal, always.

Use this link https://docs.oracle.com/javase/tutorial/essential/regex/
Oracle Tutorials: "Lesson: Regular Expressions".

Spyros
  • 339
  • 1
  • 7
  • This is an old question, since then I've been trying to follow Shai's suggestion of overriding Constraint's isValid method, rather than using regex, so that I have easily readable code. Anyway I thank you for the answer, because it may come in handy. – Francesco Galgani Jul 08 '21 at 06:15
  • @FrancescoGalgani Well, I used to have the same issue as Shai had (has?) with RegEx, but insisted attempting to use the functionality provided by Java SE. Coding from scratch is not productive in my view, even if you manage to address the maintainability, complexity and performance issues of such an approach. – Spyros Jul 08 '21 at 07:51
  • Note that "the problem seems to be in the initialization of the Constraint object" is the only think which I consider to go beyond a link-only answer. But that is sufficient.... – Yunnosch Jul 12 '21 at 13:09
  • I am not sure what @Yunnosch means in his previous comment. However thanks for trying to make the answer more readable. The issue is that different search engines do not return same results for the same term. It is a matter of idempotence but at the same time, mentioning the specific search engine may violate rules... – Spyros Jul 12 '21 at 13:21
  • If the search is so tricky that the engine is relevant then stick with the link. Links themselves are not a problem. Just posts which ONLY consist of a link (and "advertisment" for, not blaming you) are considered not an answer. I propose to remove the search recommendation or leave it generic and in. It is not necessary for any user to find exactly the same site you mean, as long as they find something on the search topic, which you proposed sensibly. You already got an upvote, you may get more if you do more explaining and/or summarising what you linked. I.e. have the solution here AND link. – Yunnosch Jul 12 '21 at 13:30
  • Oh, by the way. I only try to convince you. If I cannot, then you are free to undo my edits. I promise to then leave it and even apologize for going beyond what edits should do. I am however convinced that my edit slightly improved your post and that more explanation added by you would improve it much more, which could get you more upvotes. – Yunnosch Jul 12 '21 at 13:37
  • @Yunnosch I will edit and just leave the link if it is considered legit. It opens the official Oracle Tutorials for the specific matter. There may be other tutorials, especially articulated in a more simple language, but I consider the referred webpage a more fair reference so as to avoid being considered a promotion of one website against another. – Spyros Jul 12 '21 at 14:14