Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice: Input Validation (Android)

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,

  1. Can we create one java file for validation purpose only? All input form ,must go to that one validation file only (In case of many input page screen in one apps). If YES, how can I get an example/link/tutorial of that technique for my learning study. If NO, why?

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.

like image 337
Salehin Rafi Avatar asked Oct 12 '15 02:10

Salehin Rafi


People also ask

What is the easiest way to validate user inputs in Android?

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.

Which is best practice in input validation?

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.

What is proper input validation?

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.

Where should input validation occur?

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.


1 Answers

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 */
    }
});
like image 198
Randyka Yudhistira Avatar answered Sep 28 '22 12:09

Randyka Yudhistira