SHARE
TWEET

input validation

a guest Jan 19th, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.      * Confirms that input from user is valid for registration
  3.      * @param invoice_address Input array
  4.      * @return String with specific error message if invalid input, null if valid input
  5.      */
  6.     static public String validate_input (ArrayList<String> invoice_address) {
  7.  
  8.         // Here I assume that fields that hasn't been entered is considered empty Strings
  9.         // rather than null objects. If that was the case, then null checking has to be performed:
  10.         // If String is null, then set to empty String (which results in error message)
  11.  
  12.         int countryInt;
  13.         String address = invoice_address.get(0);
  14.         String zipCode = invoice_address.get(1);
  15.         String countryID = invoice_address.get(2);
  16.         String address2 = invoice_address.get(3);
  17.         String city = invoice_address.get(4);
  18.  
  19.         // Since TechShip has customers from all over the world, with different languages and
  20.         // address formats, I opted for simply checking the length of the String rather than
  21.         // matching with regular expressions. One improvement would be allowing special
  22.         // characters but no digits
  23.  
  24.         // Confirm that address is at least three characters long
  25.         if (address.length() < 3 )
  26.             return "Please enter valid primary address";
  27.  
  28.         // Confirms that zip code is only digits and at least three numbers
  29.         if (!zipCode.chars().allMatch(Character::isDigit) || zipCode.length() < 3)
  30.             return "Please enter valid zip code";
  31.  
  32.         // Parsing countryID String to int. If null, set to 0 (results in error message)
  33.         try {
  34.             if(countryID != null)
  35.                 countryInt = Integer.parseInt(countryID);
  36.             else
  37.                 countryInt = 0;
  38.         } catch (NumberFormatException e) {
  39.             countryInt = 0;
  40.         }
  41.  
  42.         // Confirms that a country in the list has been selected (range of 1-250)
  43.         if (countryInt < 1 || countryInt > 250)
  44.             return "Please select country";
  45.  
  46.         // Confirms that Address 2 field is either empty or at least three characters
  47.         if (!address2.isEmpty() && address2.length() < 3)
  48.             return "Please enter valid secondary address";
  49.  
  50.         // Confirms that city is at least three characters
  51.         if (city.length() < 3)
  52.             return "Please enter valid city";
  53.  
  54.         // Returns null if valid input
  55.         return null;
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top