java Android的表单验证库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6613321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form validation library for Android?
提问by TN.
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.
是否有适用于 Android 的成熟表单验证 API / 库?我找到了http://code.google.com/p/android-binding/但它似乎正在大力开发中。
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 library that allows me to declaratively specify validators (e.g. directly in XML or in code using annotations or by functional fluent way, ...).
更新:只是为了澄清我的问题。目前,我已经强制进行了硬编码的表单验证。而且我想知道,是否有一个成熟的表单验证库允许我以声明方式指定验证器(例如,直接在 XML 中或在代码中使用注释或通过功能流畅的方式,...)。
回答by Ragunath Jawahar
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.,
顺序注释是可选的,它指定字段验证的顺序。仅当您希望在验证期间保留字段的顺序时才需要这样做。还有其他一些注解,例如@Email
,@IpAddress
,@Isbn
等,
Android Studio / Gradle
Android Studio / Gradle
compile 'com.mobsandgeeks:android-saripaar:2.0.2'
Check for the latest available version.
检查最新的可用版本。
Eclipse
You can download the jar from hereand add it to your Android libs
directory.
Eclipse
您可以从这里下载 jar并将其添加到您的 Androidlibs
目录中。
Old Answer (Saripaar v1)
I have authored a library for validation. Here is the associated blogand 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.
旧答案 (Saripaar v1)
我编写了一个用于验证的库。这是相关的博客和项目。我已经成功地在生产应用程序中使用它,它目前满足我们在 Android 验证表单中面临的大多数常见场景。有些规则是开箱即用的,如果您需要编写自己的规则,可以通过编写自己的规则来实现。
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.
也有or
和and
规则,允许用户执行&&
和||
操作上的一些规则。还有一个compositeOr
andcompositeAnd
规则允许您在多个视图之间执行验证。
If any of those seem to be insufficient, you can always write your own rule by extending the Ruleclass.
回答by Roman K
I would recommend OVal
我会推荐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.
我打算在我的下一个项目中使用它,因为它有多种表达语言,但只需要 Java。
It is not JSR303 compilant, but supports those annotations too.
它不是 JSR303 编译器,但也支持这些注释。
回答by Muhammad Aamir Ali
Use Oval library for validation in android It is object based validation library used in Object classes
在android中使用Oval库进行验证它是Object类中使用的基于对象的验证库
回答by gorefest
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.htmlfor details.
由于我自己解决了这些问题,因此我将此功能添加到了我的 BARACUS 应用程序框架的核心功能中。有关详细信息,请参阅http://baracusframework.blogspot.de/2014/01/baracus-from-scratch-part-6-automatic.html。
The concept includes a declarative binding of named validators in the form plus the automatic routing of any constraint violation messages.
该概念包括形式中命名验证器的声明性绑定以及任何约束违规消息的自动路由。
回答by Daud Arfin
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.
如果您想验证文本字段(EditText),那么我建议您使用带有正则表达式的 Pattern 和 Matcher,当然我们这里没有使用 android API,但 android 支持 java API,因此您可以。
回答by Marcus Wolschon
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.
或myTextView.setError(error-message)
在onTextChangedListener
工作正常,第二个看起来真的很棒。
回答by Daud Arfin
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 .