String Validation in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ritesh Muhatte
    New Member
    • Oct 2011
    • 2

    String Validation in Java

    In Java I want to validate a account number for a bank which will be the input be the use.It should have the following fomat:
    1)It should start with SB
    2)After SB it should be followed by number 2011
    3)followed by a 4 digit number.
    4)It should not be greater than 10 digit
    eg:SB20115789
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I recommend that you use Regular Expressions to validate the string containing the account number.

    -Frinny

    Comment

    • Ritesh Muhatte
      New Member
      • Oct 2011
      • 2

      #3
      What do u mean by regular expressions?
      Please explain.
      Last edited by Frinavale; Oct 11 '11, 04:13 PM. Reason: Corrected spelling of the words "what" and "please"

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        "Regular Expressions (wiki link)" are used for string comparisons in programming. It uses a "formal language" (special type of syntax) to describe a "pattern" that you want to compare your string to.

        In your case, you have a pattern that you want to check:
        1. The string should start with SB
        2. After SB the string should contain the number "2011"
        3. Followed by a 4 digit number.
        4. The string should not be greater than 10 digit


        What you need to do is translate this pattern into the Regular Expression syntax. That way you can use a Regular Expression to check if the String matches the pattern you described. If it matches then the string is valid, otherwise it's not valid.

        The topic is quite large and the syntax varies slightly between programming languages.

        Please refer to the Java Documentation on Regular Expressions to learn about how to use them in your application.

        -Frinny
        Last edited by Frinavale; Oct 11 '11, 04:16 PM.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          In Regular Expression Syntax, when you want to check if a string "starts with" something you use the ^ character at the beginning of the string.

          In your case you want to check if the String starts with "SB2011". So, to translate this into Regular Expression syntax you would have "^SB2011".

          Now you need 4 digits to follow this. There are a couple of ways to do this but I think I'll demonstrate how to do this using a range.

          A range is declared inside square brackets ([...]). If you want a lower case character between "a" and "z" then your range would be "[a-z]", if you want an upper case character between "A" and "Z" the range would be "[A-Z]". If you want a number that falls in the range between 0 - 9 the regular expression syntax would be "[0-9]".

          So, now you know how to check for a digit...we can add this to your Regular Expression. The following will check if the string starts with "SB2011" and checks if this is followed by a single digit:
          "^SB2011[0-9]"

          But you need to check if 4 digits follow the "SB2011"...
          You could insert 4 ranges from 0-9 like this:

          "^SB2011[0-9][0-9][0-9][0-9]"

          Or you could use the repetition syntax in Regular Expressions. If you want to check if something repeats you use curly braces followed by the number of times that that something repeats. For example if you want to check if a digit repeats 4 times you would have your range followed by "{4}".

          In your case you would have:
          "^SB2011[0-9]{4}"

          I'm not too sure how you would check the length of a String using Regular Expressions...i t's been a while since I've seriously used them.

          But if your string can only contain "SB2011#### " then you can check to see if the string "ends with" something using the $ character in your regular expression.
          So, "^SB2011[0-9]{4}$" will match a string that only contains "SB2011#### " (from start to end). The string cannot contain anything else.

          -Frinny
          Last edited by Frinavale; Oct 11 '11, 04:18 PM.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            So, apparently in Java you need to declare a Pattern type based on the string that contains the regular expression.

            You have to "compile" this Pattern in order to use it with a Matcher. The Matcher is the thing that will compare the String to the Regular Expression pattern.

            So, you need to declare a Pattern based on a string that contains regular expression pattern that you want to check. And you need to "compile" that Pattern. Then you need to declare a new Matcher based on the compiled Pattern.

            Like this:
            Code:
             Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
             Matcher m = p.matcher(str); //str is the String that needs validating
            Now you can call the Matcher's "matches()" method to see if the string you want to validate "matches" the pattern that you are expecting:

            Code:
            public boolean ValidateAccountNumber(string str)
            {
             Pattern p = Pattern.compile("^SB2011[0-9]{4}$");
             Matcher m = p.matcher(str); //str is the String that needs validating
             return m.matches();
            }

            Pretty clean isn't it :)

            -Frinny
            Last edited by Frinavale; Oct 11 '11, 04:21 PM.

            Comment

            • Tassos Souris
              New Member
              • Aug 2008
              • 152

              #7
              One little note to it... you can make the pattern static member... in this way you avoid compiling the pattern every time you call the ValidateAccount Member() method...

              Comment

              Working...