I am new to android mobile development (Android Studio native development - for new knowledge). And here I want to ask a question regarding best practice for input validation. As far we know, when a developer develop input form. We need to prevent a user from key in a wrong input into the text field. So here is my question,
From my point of view personally, it should have a way to implement the technique. So that we didn't need to reuse same code all over again for each java file (in term of clean code). Unfortunately, I didn't find any example or tutorial for that. Maybe I search a wrong keyword or misread. And if there is no such technique exist, what are the best practice for input validation?
Thank you.
p/s: This thread for find a better way in best practice. Thank you.
In the validator package, create a class named EmptyValidator as below; This class will take a string value as user input and extends BaseValidator class. In validate() method we control the input is empty or not then we return ValidateResult value according to the validation result.
The best position for validationInstant validation is best positioned to the right hand side of the input, or failing that immediately below. This works best in principle for creating the conversation between user and for that manages to gamify the often tedious process of form inputs.
Input validation (also known as data validation) is the proper testing of any input supplied by a user or application. Input validation prevents improperly formed data from entering an information system. Input validation can be carried out on client-side and server-side code to reinforce infrastructure security.
In general, it is best to perform input validation on both the client side and server side. Client-side input validation can help reduce server load and can prevent malicious users from submitting invalid data.
This java class implements a TextWatcher
to "watch" your edit text, watching any changes done to the text:
public abstract class TextValidator implements TextWatcher {
private final TextView textView;
public TextValidator(TextView textView) {
this.textView = textView;
}
public abstract void validate(TextView textView, String text);
@Override
final public void afterTextChanged(Editable s) {
String text = textView.getText().toString();
validate(textView, text);
}
@Override
final public void
beforeTextChanged(CharSequence s, int start, int count, int after) {
/* Needs to be implemented, but we are not using it. */
}
@Override
final public void
onTextChanged(CharSequence s, int start, int before, int count) {
/* Needs to be implemented, but we are not using it. */
}
}
And in your EditText
, you can set that text watcher to its listener
editText.addTextChangedListener(new TextValidator(editText) {
@Override public void validate(TextView textView, String text) {
/* Insert your validation rules here */
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With