开发者

Form validation library for Android?

Is there any mature form validation API / library for Android? I've found http://code.google.com/p/android-binding/ but it seems that is under heavy development.

UPDATE: Just to clarify my question. Currently, I have hardcoded form validation imperatively. And I would like to know, if there is a mature form validation libra开发者_如何学编程ry that allows me to declaratively specify validators (e.g. directly in XML or in code using annotations or by functional fluent way, ...).


The library now supports annotations, you can validate your fields just by adding them. Here is an example code snippet.

@NotEmpty
@Order(1)
private EditText fieldEditText;

@Checked(message = "You must agree to the terms.")
@Order(2)
private CheckBox iAgreeCheckBox;

@Length(min = 3, message = "Enter atleast 3 characters.")
@Pattern(regex = "[A-Za-z]+", message = "Should contain only alphabets")
@Order(3)
private TextView regexTextView;

@Password
@Order(4)
private EditText passwordEditText;

@ConfirmPassword
@Order(5)
private EditText confirmPasswordEditText;

The order annotation is optional and specifies the order in which the fields should be validated. This is ONLY required if you want the order of the fields to be preserved during validation. There are also other annotations such as @Email, @IpAddress, @Isbn, etc.,

Android Studio / Gradle

compile 'com.mobsandgeeks:android-saripaar:2.0.2'

Check for the latest available version.

Eclipse
You can download the jar from here and add it to your Android libs directory.

Old Answer (Saripaar v1)
I have authored a library for validation. Here is the associated blog and the project. I have sucessfully used it in production applications and it currently satisfies most of the common scenarios that we face in validation forms for Android. There are rules that come out of the box and if you need to write your own, you can do that by writing your own Rule.

Here is a snippet that illustrates the use of the library.

validator.put(nameEditText, Rules.required("Name is required."));
validator.put(nameEditText, Rules.minLength("Name is too short.", 3));
validator.put(emailEditText, Rules.regex("Email id is invalid.", Rules.REGEX_EMAIL, trim));
validator.put(confirmPwdEditText, Rules.eq("Passwords don\'t match.", pwdEditText);

There are also or and and rules that allow you to perform && and || operations on several rules. There is also a compositeOr and compositeAnd rule that allows you to perform validations between several Views.

If any of those seem to be insufficient, you can always write your own rule by extending the Rule class.


I would recommend OVal

public class BusinessObject {

@NotNull
@NotEmpty
@Length(max=32)
private String name;

 ...
}

// collect the constraint violations
List<ConstraintViolation> violations = validator.validate(bo);

I'm planing to uses it in my next project since is has a variety of expression languages, but only Java is hard required.

It is not JSR303 compilant, but supports those annotations too.


If you mean to validate text fields(EditText) then i will suggest you to use Pattern and Matcher with regex expressions, ofcourse we are not using android API here but android support java API hence you can.


This will be the simple and mature way to validate the input texts or forms:

private EditText et_first_name;
et_first_name = (EditText)findViewById(R.id.et_first_name);
et_first_name = (EditText)findViewById(R.id.et_first_name);
first_name.matches("^(?i)(?=.{1,20}$)([A-Za-z]+[A-Za-z0-9-_.]*$)"); // use the regex expression inside brackets and returns the error status .


Use Oval library for validation in android It is object based validation library used in Object classes


since I fought with such problems on my own, I added this functionality to the core functions of my BARACUS application framework. See http://baracusframework.blogspot.de/2014/01/baracus-from-scratch-part-6-automatic.html for details.

The concept includes a declarative binding of named validators in the form plus the automatic routing of any constraint violation messages.


Using either

android.widget.AutoCompleteTextView.Validator;
myTextview.setValidator(myCustomValidator);

or myTextView.setError(error-message) in an onTextChangedListener works fine and the second one looks really great.


You can use FormValidateor Library - https://android-arsenal.com/details/1/8353. It works with jetpack compose and view system.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜