I have an XML layout file that contains an EditText
and a Button
. I would like to display the following validation message to provide feedback to the user:
You must enter 4 numbers
What's the best way to go about accomplishing this?
When you have validation enabled, you can receive messages that validate a filter or function expression. You can receive either warnings or errors based on the information that you have specified in your mapping specification.
Add a ValidationSummary control to the page at the location where you want to display the collected error messages. Set the ErrorMessage and Display properties of the individual validation controls. (Default) Each error message appears as a bulleted item. Each error message appears on its own line.
ValidationMessage(HtmlHelper, String) Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
In order to display error messages on forms, you need to consider the following four basic rules: The error message needs to be short and meaningful. The placement of the message needs to be associated with the field. The message style needs to be separated from the style of the field labels and instructions.
From my experience, this is the best way:
EditText yourEditText;
// when you detect an error:
yourEditText.setError("Input must be 4 digits and numeric");
The result is:
Also, if the input must be numeric, use android:inputType="numberSigned"
in the EditText
definition. That way the device won't allow the user to put non-numeric values; even better, it will show a special keyboard to do so:
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