Application Techniques



Validating with String Pattern Matching

How to use string pattern matching to perform data-entry validation.

About this technique

Details

Category

Core Programming Techniques> General

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Walking through the overall matching process   Top of page

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:

  1. It compares string lengths.

  2. On a character by character basis, it does the following:

    Action

    Description

    Checks the pattern string for pattern-specific wildcards

    If a wildcard character is entered, the code tests the corresponding character in the string entry as to whether it meets the criterion for that particular character pattern. If the string character does not meet the criterion of the pattern character, the code displays a pattern-specific error message.

    Example    If the user enters the character "#" as the third character in the pattern string, this specifies that the third character in the entry string must be a digit (such as "8") for the character match to succeed.

    Order    Here's the order in which the code checks for pattern-specific wildcards:

    • any-upper-case wildcard: 'A'

    • any-lower-case wildcard: 'a'

    • any-letter wildcard: 'z'

    • any-digit wildcard: '#'

    • any-alphanumeric value wildcard: '@'

    • any-character wildcard: '*'

    Compares characters

    If the string character and the pattern character are not equivalent, the code displays a general character-mismatch error message.

Comparing strings by length   Top of page

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; 
  } 

Notes about the code

Comparing strings character by character   Top of page

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':    
          } 
        } 

Notes about the code

Checking for uppercase and lowercase characters   Top of page

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; 

Notes about the code

Checking for letter characters   Top of page

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; 

Notes about the code

Checking for digit characters   Top of page

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; 

Notes about the code

Checking for letter or digit characters   Top of page

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; 

Notes about the code

Checking for the any-character wildcard   Top of page

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; 

Checking for a character match   Top of page

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; 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.