How to use string pattern matching to perform data-entry validation.
| |
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server |
In this example, the user provides a pattern as the basis of string comparison. Specific characters are used as pattern-specific wildcards (such as any numeral, any uppercase character, and so on). The code parses the pattern string for the wildcard characters and handles each corresponding entry character accordingly. It uses several string pattern matching techniques, in the following order:
The following code gets the pattern string and the entry string from text fields. It compares their lengths. If the lengths are different, it displays a dialog box with an error message that details the two different lengths.
//Code fragment in tbValidate button's actionPerformed event String pattern = fldPattern.getText(); String string = fldString.getText(); int length = string.length(); if (length != pattern.length() ) { agDialog.showMessage("Invalid String", "The length of the string (" + length + ") does not equal the length of the pattern (" + pattern.length() + "). " + "Please correct the string or pattern and try again."); fldString.requestFocus(); return; }
if
statement to check if the length of the pattern string and the length of the entry string are different.
error.showMessage()
displays the dialog box. Its second parameter is a concatenated string indicating the length of the entry string and the length of the pattern string.
The following code loops through each character in the pattern string. The case statements in the following sections compare the character to each of the pattern-specific wildcard characters.
//Code fragment in tbValidate button's actionPerformed event for (int i = 0; i < length; i++) { switch (pattern.charAt(i) ) { // case statements belong here'A': } }
charAt()
returns the current character in the pattern string. The for
loop ensures that every character in the pattern string is checked.
The following code uses case statements to check if an upper- or lowercase wildcard character is used in the pattern. In the case where the upper-case wild case is used, it checks that the entry character is in the upper case. If it is not, showMessage()
displays an error message. The code for checking for lower-case characters is similar.
//Code fragment in tbValidate button's actionPerformed event case 'A': if (!Character.isUpperCase(string.charAt(i) ) ) { agDialog.showMessage("Invalid String", "The character \"" + string.charAt(i) + "\" at position " + (i + 1) + " is not an uppercase letter. " + "Please correct it and try again."); fldString.requestFocus(); return; } break; case 'a': if (!Character.isLowerCase(string.charAt(i) ) ) { agDialog.showMessage("Invalid String", "The character \"" + string.charAt(i) + "\" at position " + (i + 1) + " is not a lowercase letter. " + "Please correct it and try again."); fldString.requestFocus(); return; } break;
isUpperCase()
returns true if the corresponding entry character is an uppercase character.
isLowerCase()
returns true if the corresponding entry character is a lowercase character.
The following code checks if a letter wildcard character is used in the pattern. In this case, it checks that the entry character is a letter. If it is not, showMessage() displays an error message.
//Code fragment in tbValidate button's actionPerformed event case 'z': if (!Character.isLetter(string.charAt(i) ) ) { agDialog.showMessage("Invalid String", "The character \"" + string.charAt(i) + "\" at position " + (i + 1) + " is not a letter. " + "Please correct it and try again."); fldString.requestFocus(); return; } break;
isLetter()
returns true if the corresponding entry character is a letter character.
The following code checks if a digit wildcard character is used in the pattern. In this case, it checks that the entry character is a digit. If it is not, showMessage()
displays an error message.
//Code fragment in tbValidate button's actionPerformed event case '#': if (!Character.isDigit(string.charAt(i) ) ) { agDialog.showMessage("Invalid String", "The character \"" + string.charAt(i) + "\" at position " + (i + 1) + " is not a number. " + "Please correct it and try again."); fldString.requestFocus(); return; } break;
isDigit()
returns true if the corresponding entry character is a digit character.
The following code checks if a letter-or-digit wildcard character is used in the pattern. In this case, it checks that the entry character is either a letter or a digit. If it is neither (for example, the user could have entered "=" or "$"), showMessage()
displays an error message.
//Code fragment in tbValidate button's actionPerformed event
case '@':
if (!Character.isLetterOrDigit(string.charAt(i) ) )
{
agDialog.showMessage("Invalid String",
"The character \"" + string.charAt(i) +
"\" at position " + (i + 1) + " is not alphanumeric. " +
"Please correct it and try again.");
fldString.requestFocus();
return;
}
break;
isLetterOrDigit()
returns true if the corresponding entry character is a either a letter or a digit character.
The following code checks if the any-character wildcard character is used in the pattern. In this case, it lets all characters through. There are no error messages for this case.
//Code fragment in tbValidate button's actionPerformed event case '*': // Anything goes here, so just pass it on through. break;
The following code shows the default case. This code is run only if none of the wildcard characters were entered. The code checks if the two characters (the pattern character and its corresponding entry character) are equivalent. If they are not, showMessage()
displays a general error message.
default: if (pattern.charAt(i) != string.charAt(i) ) { agDialog.showMessage("Invalid String", "The character \"" + string.charAt(i) + "\" at position " + (i + 1) + " does not match the pattern \"" + pattern.charAt(i) + "\". "+ "Please correct it and try again."); fldString.requestFocus(); return; } break;