我正在尝试在android中找到一个表单验证库。有这样的事吗?
我有一个注册表单,我想验证其字段。如果用户输入了无效数据,我想在字段右侧放置一个红色警告标记,然后弹出一个他输入无效数据的工具提示。
我知道android:inputType
,但这不是我想要的
答案 0 :(得分:2)
我不知道任何这样的图书馆。但是,如果您正在使用EditTexts,那么您最好的选择是使用自定义TextWatcher:
class TextCheck implements TextWatcher
{
private EditText editor;
public TextCheck(EditText editor)
{
this.editor = editor;
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// check the text, and if the user entered
// something wrong, change your edittext
if(something wrong)
{
editor.setBackgroundColor(Color.RED); //for example
}
}
@Override
public void afterTextChanged(Editable arg0){}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3){}
}
然后你可以在所有的EditTexts上使用它,比如
EditText editor = (EditText) findViewById(...your id...);
editor.addTextChangedListener(new TextCheck(editor));
答案 1 :(得分:1)
有这样的事吗?
啊..是的,有一个,你可以找到它here。
它确实为您进行了验证,使用但不限于注释。要了解库的功能,请访问以下answer on SO,其中我描述了库的用法。
如果您想编写新规则,可以随时扩展Rule类。
PS:我是图书馆的作者。
答案 2 :(得分:1)
我知道这已经过时了,但您可以尝试这个优秀的Android Validation library并访问此Stackoverflow reference和此Stackoverflow reference作为使用示例,因为我发现主要的图书馆有点难以理解了解。
答案 3 :(得分:0)
我做了类似的事情。您可以改进此代码并根据您的需要进行调整。
<强> EditTextWithValidation.java 强>
public class EditTextWithValidation extends EditText implements OnTouchListener {
private EditTextValidator mValidator;
public EditTextWithValidation(Context context) {
super(context);
initialize();
}
public EditTextWithValidation(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public EditTextWithValidation(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
public EditTextValidator getCustomValidator() {
return mValidator;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
setError(null);
return false;
}
private void initialize() {
mValidator = new EditTextValidator(this);
setOnTouchListener(this);
}
}
<强> EditTextValidator.java 强>
public class EditTextValidator {
private static final String TAG = EditTextValidator.class.getName();
private enum ValidationResult {
Ok, Rules, Mismatch
}
private EditText mParent;
private Pattern mValidationPattern;
private int mValidationErrorMsgId;
private boolean mAllowEmpty;
private EditText mMatchView;
private int mMismatchMsgId;
private int mMinLength;
private int mMaxLength;
private ValidationResult mValidationResult;
public EditTextValidator(EditText parent) {
this.mParent = parent;
}
public void setAllowEmpty(boolean allowEmpty) {
this.mAllowEmpty = allowEmpty;
}
public void setValidationErrorMsgId(int validationErrorMsgId) {
this.mValidationErrorMsgId = validationErrorMsgId;
}
public void setValidationRules(String strPattern, int validationErrorMsgId, boolean allowEmpty) {
try {
if (!TextUtils.isEmpty(strPattern)) {
mValidationPattern = Pattern.compile(strPattern);
}
} catch (PatternSyntaxException e) {
Log.e(TAG, e.getMessage(), e);
ToastUtil.toastShort("Invalid validation pattern!");
}
this.mValidationErrorMsgId = validationErrorMsgId;
this.mAllowEmpty = allowEmpty;
}
public void setValidLength(int min, int max) {
mMinLength = min;
mMaxLength = max;
}
public void shouldMatch(EditText matchView, int mismatchMsgId) {
this.mMatchView = matchView;
this.mMismatchMsgId = mismatchMsgId;
}
public boolean validate() {
mValidationResult = ValidationResult.Ok;
InputMethodManager imm = (InputMethodManager) mParent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mParent.getWindowToken(), 0);
final String text = mParent.getText().toString();
final int length = text.length();
if (mValidationResult == ValidationResult.Ok && !mAllowEmpty && 0 == text.length()) {
mValidationResult = ValidationResult.Rules;
}
if (mValidationResult == ValidationResult.Ok) {
if (mMinLength != 0 && length < mMinLength) {
mValidationResult = ValidationResult.Rules;
} else if (mMaxLength != 0 && length > mMaxLength) {
mValidationResult = ValidationResult.Rules;
}
}
if (mValidationResult == ValidationResult.Ok && mValidationPattern != null) {
Matcher m = mValidationPattern.matcher(text);
if (!m.matches())
mValidationResult = ValidationResult.Rules;
}
if (mValidationResult == ValidationResult.Ok && mMatchView != null) {
if (mMatchView.getText().toString().compareTo(text) != 0)
mValidationResult = ValidationResult.Mismatch;
}
if (ValidationResult.Ok == mValidationResult) {
mParent.setError(null);
} else {
CharSequence error = null;
if (ValidationResult.Rules == mValidationResult)
error = MyApplication.getContext().getText(mValidationErrorMsgId);
else if (ValidationResult.Mismatch == mValidationResult)
error = MyApplication.getContext().getText(mMismatchMsgId);
mParent.setError(error);
mParent.requestFocus();
}
return mValidationResult == ValidationResult.Ok;
}
}
<强>用法:强>
mSignupEmail = (EditTextWithValidation) root.findViewById(R.id.signup_email);
mSignupEmail.getCustomValidator().setValidationRules(
"[a-zA-Z0-9_-]+(?:\\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9_-]+)*\\.(?:[a-zA-Z]{2,})",
R.string.email_answer_validation_msg,
false);
mSignupEmail.getCustomValidator().setValidLength(0, 50);
…
mSignupPassword = (EditTextWithValidation) root.findViewById(R.id.signup_password);
mSignupPassword.getCustomValidator().setValidationRules(
"[a-zA-Z0-9!@#$%^&*()]{6,20}",
R.string.password_validation_msg,
false);
…
mSignupConfirmPassword = (EditTextWithValidation) root.findViewById(R.id.signup_confirm_password);
mSignupConfirmPassword.getCustomValidator().setAllowEmpty(true);
mSignupConfirmPassword.getCustomValidator().shouldMatch(mSignupPassword, R.string.password_mismatch);
mSignupConfirmPassword.getCustomValidator().setValidationErrorMsgId(R.string.password_validation_msg);
…
if (mSignupEmail.getCustomValidator().validate() && mSignupPassword.getCustomValidator().validate() && mSignupConfirmPassword.getCustomValidator().validate()) {
// DO SOMETHING
}
答案 4 :(得分:0)
安装此应用:https://play.google.com/store/apps/details?id=com.desarrollodroide.repos
转到:Utils
- &gt; Android-Validator
- &gt; View Demo
此应用程序中还有许多其他很酷的库。好处是你可以查看每个库的演示,如果你喜欢的话,可以获得该特定库的github repo的链接。这非常有用。