Android表单输入验证库:android-validation-komensky
jopen
11年前
这是一个简单的函数库,使用注释语法来验证用户输入的表单信息。你仅可以验证几行代码,且显示的所有视图都将反馈给用户。它还带有一个可扩展的在线验证选项,这样你就可以为定制视图编写自己的验证信息及适配器了。
特性:
- Validate all views at once and show feedback to user. With one line of code.
- Live validation - check user input as he moves between views with immediate feedback.
- Extensible library - you can add your own validations or adapters for custom views.
如何验证
首先,在你的视图中的注释如下:
@NotEmpty(messageId = R.string.validation_name) @MinLength(value = 3, messageId = R.string.validation_name_length, order = 2) private EditText mNameEditText;
Now you are ready to:
FormValidator.validate(this, new SimpleErrorPopupCallback(this));
You will receive collection of all failed validations in a callback and you can present them to the user as you want. Or simply use prepared callbacks (like SimpleErrorPopupCallback
).
Live validation
To start and stop live validation, simply call:
FormValidator.startLiveValidation(this, new SimpleErrorPopupCallback(this)); FormValidator.stopLiveValidation(this);
List of all supported validation annotations
Validations supported out of the box:
@NotEmpty(messageId = R.string.validation_name, order = 1) private EditText mNameEditText;
@MinLength(value = 1, messageId = R.string.validation_participants, order = 2) private EditText mNameEditText;
@MinValue(value = 2L, messageId = R.string.validation_name_length) private EditText mEditNumberOfParticipants;
@MinNumberValue(value = "5.5", messageId = R.string.validation_name_length) private EditText mEditPotentialOfHydrogen;
@RegExp(value = EMAIL, messageId = R.string.validation_valid_email) private EditText mEditEmail;
@DateInFuture(messageId = R.string.validation_date) private TextView mTxtDate;
@DateNoWeekend(messageId = R.string.validation_date_weekend) private TextView mTxtDate;
@Custom(value = MyVeryOwnValidator.class, messageId = R.string.validation_custom) private EditText mNameEditText;