开发者

Java validators?

开发者 https://www.devze.com 2023-01-18 01:42 出处:网络
is there any Java validator libraries i can use to validate text input from an android app? i know that in spring there are validators for various different typ开发者_JAVA百科es such as checking if a

is there any Java validator libraries i can use to validate text input from an android app?

i know that in spring there are validators for various different typ开发者_JAVA百科es such as checking if a value is a int, if a value is a valid email, etc etc.

Anything similar in core java or even android?

cheers in advance. i dont want to re-invent the wheel if its already been implemented. i could always sue java reg expressions to construct my own ones but was wondering if their was one done specifically for email formats.


Normally I would suggest Hibernate Validator, but that is probably overkill for Android. If you're doing simple form input, you can write your own validation methods and use regular expressions to validate your input. There are a few regular expressions to validate emails addresses:

How to Find or Validate an Email Address


I have written some supporting classes to fix this. It contains a validator interface, an abstract inplementation,a validationresult class and 2 examples of custom implemented validations. 1 for regular expressions on text and a simple one to check if a checkbox is checked.

Here is the link to my blog containing the sources and a small bit of explaining Form validation on Android: link text


You should write your own validator methods. E.g. you can use regex pattern for strings, java regex tutorial.


Yes, I have authored a library to deal with validations on Android. It validates EditText, CheckBox, RadioButton, Spinner by default. You can also use annotations to simplify validation.

You are not limited by the built-in annotations or the rules. You can always extend the Rule class in the library to validate your custom views and rules.

It also supports asynchronous validations (like checking for a unique username from a remote server.)

You can check the project page from here.

I have also written a blog that describes how to write custom rules when the built-in rules are not sufficient.

Hope this helps.

Edit (As a clarification to jonney's comment)

Yes, what you have presented makes sense. To register a new account and validate in the same web service call could take n calls to register a new account, however the example I have depicated in the blog would take n + 1 calls to register a new account. That example however just showcases the capabilities of the Validator.

To accomplish what you are looking for, you can just write a new rule.

Rule<EditText> registerIfUniqueRule = new Rule<EditText>("Username must be unique.") {

    public boolean isValid(EditText view) {
        String username = view.getText().toString();
        boolean accountRegistered = false;

        // Make a call to your web service
        // Mode code…

        return accountRegistered;
    }
}

Now add this rule to your Validator instance.

validator.put(usernameEditText, registerIfUniqueRule);

And from any of your EventListeners call the async validation method.

validator.validateAsync();

You are done ;) The validator may have limitations but not in the case you have mentioned, we'll think of new ways to use the API once we get accustomed to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号