How to Error Check Fields in Java
- 1). Determine the type of data the Java form field requires. For example, your application may require text or numeric input. Establish rules for the analysis of this data, such as allowing only alphanumeric characters or numbers from one to 100. When designing an application, accept only valid information and block all invalid information. Document these rules inside the Java application's source code using the syntax "/* Comment Text */."
- 2). Access the form object in your Java program's code. If your form is named "inptPhone," then you would type "String value = inptPhone.text."
- 3). Check the form and verify it contains an acceptable value. Ensure your form only contains numbers with the following code:
/* Check to see if the value is empty, as this indicates a blank submission */
public boolean numericCheck(String value) {
if(value == null || value.length() == 0)
return false;
for(int i= 0; i < value.length(); i++) {
// check for non-numeric values
if(!Character.isDigit(value.charAt(i)))
return false;
} //return true if all checks passed and we have not prematurely exited the function
}
Source...