Thread: Check NRIC

    #1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0

    Check NRIC


    how do i do a check to see if the user input of NRIC is in S1234567D format. 1st char has to be either S or F, then follow by 7 digits and last char has to be a alphabet from a to z.

    Codes would be helpful. thanks.

    pls help asap.
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Easiest way is going to be regex.
    [CODE]
    import java.util.regex.*;

    if ( Pattern.matches( "^[SF][0-9]{7}\p{Alpha}$", request.getParameter( "inputString" ) ) ) {
    .. do code ..
    } else {
    .. no match ..
    }

    If you're going to be doing multiple matches you'll want to compile the pattern first. See http://java.sun.com/j2se/1.4/docs/ap...x/Pattern.html for instructions.
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    currently i am using the 1.3.1 api. it does not have regex. so what do i do??
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    upgrade

    You're going to have to test properties on the string, then.
    string.length() == 9
    string.substring( 0, 1 ) == "S" || string.substring( 0, 1 ) == "F"
    try {
    Integer.parseInt( string.substring( 1, 9 ) );
    } catch ( NumberFormatException nfe ) {
    ... these are not the numbers you are looking for, move along ...
    }
    string.substring( 8, 9 ).toUpperCase() > "A" && string.substring( 8, 9 ).toUpperCase() < "Z"

    I'm not sure about that last one. You may need to convert it to a character or something first.
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    i have tried ur method.
    there is smthing wrong with the middle 7 chars of the NRIC.

    What i have now is:
    it should check the nric is valid.
    if it is valid, it prints "Ok".
    however, now if the user types in "SdfgewgfW", as you can see the middle 7 chars are not integers. other than printing out "Middle 7 chars are not DIGITS", it prints out "Middle 7 chars are not DIGITS. Ok."

    which means it also prints out the statement for valid nric.

    Pls help asap...
  10. #6
  11. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Code:
    boolean valid = false;
    try {
      if ( !string.length() == 9 ) {
        throw new Exception( "Invalid Length" );
      }
      if ( !string.substring( 0, 1 ) == "S" ||   !string.substring( 0, 1 ) == "F" ) {
       throw new Exception( "Invalid First Letter" );
      }
      Integer.parseInt( string.substring( 1, 9 ) );
      if ( !string.substring( 8, 9 ).toUpperCase() > "A" && !string.substring( 8, 9 ).toUpperCase() < "Z" ) {
       throw new Exception( "Invalid Last Letter" );
      }
      valid = true;
    } catch ( NumberFormatException nfe ) {
      out.print( "Wrong Middle Section" );
    } catch ( Exception e ) {
      e.getMessage();
    }
    if ( valid ) {
      out.print( "welcome" );
    }
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    i can't get it still..
    and i have already done this, but the middle 7 chars check still doesn't work..

    pls suggest..

    Codes
    ___________________________________________


    //check NRIC length = 9
    if (nric.length()==9)
    {
    //check NRIC 1st char = uppercase "S" or "F"
    if(nric.substring(0,1).equals("S") || nric.substring(0,1).equals("F"))
    {
    //check NRIC middle 7 chars are digits
    try
    {
    Integer.parseInt(nric.substring(1,8));
    }
    catch ( NumberFormatException nfe )
    {
    out.println("Middle 7 chars are not DIGITS.");
    }
    //check NRIC last char = uppercase "A" to "Z"
    if(nric.substring(8,9).equals("A") || nric.substring(8,9).equals("B") || nric.substring(8,9).equals("C") || nric.substring(8,9).equals("D") || nric.substring(8,9).equals("E") || nric.substring(8,9).equals("F") || nric.substring(8,9).equals("G") || nric.substring(8,9).equals("H") || nric.substring(8,9).equals("I") || nric.substring(8,9).equals("J") || nric.substring(8,9).equals("K") || nric.substring(8,9).equals("L") || nric.substring(8,9).equals("M") || nric.substring(8,9).equals("N") || nric.substring(8,9).equals("O") || nric.substring(8,9).equals("P") || nric.substring(8,9).equals("Q") || nric.substring(8,9).equals("R") || nric.substring(8,9).equals("S") || nric.substring(8,9).equals("T") || nric.substring(8,9).equals("U") || nric.substring(8,9).equals("V") || nric.substring(8,9).equals("W") || nric.substring(8,9).equals("X") || nric.substring(8,9).equals("Y") || nric.substring(8,9).equals("Z"))
    {
    out.println("Ok.");
    }
    else
    {
    out.println("NRIC last char should be a UPPERCASE ALPHABET.");
    }
    }
    else
    {
    out.println("NRIC first char should be UPPERCASE S or F.");
    }
    }
    else
    {
    out.println("NRIC total length not = 9.");
    }
    }

    ____________________________________________________
  14. #8
  15. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    You're going to need to move the try/catch block so that it surrounds the whole thing. Otherwise, the exception is thrown, caught, and everything else just proceeds like normal.


    try
    {
    //check NRIC length = 9
    if (nric.length()==9)
    {
    //check NRIC 1st char = uppercase "S" or "F"
    if(nric.substring(0,1).equals("S") || nric.substring(0,1).equals("F"))
    {
    //check NRIC middle 7 chars are digits
    Integer.parseInt(nric.substring(1,8));
    //check NRIC last char = uppercase "A" to "Z"
    if(nric.substring(8,9).equals("A") || nric.substring(8,9).equals("B") || nric.substring(8,9).equals("C") || nric.substring(8,9).equals("D") || nric.substring(8,9).equals("E") || nric.substring(8,9).equals("F") || nric.substring(8,9).equals("G") || nric.substring(8,9).equals("H") || nric.substring(8,9).equals("I") || nric.substring(8,9).equals("J") || nric.substring(8,9).equals("K") || nric.substring(8,9).equals("L") || nric.substring(8,9).equals("M") || nric.substring(8,9).equals("N") || nric.substring(8,9).equals("O") || nric.substring(8,9).equals("P") || nric.substring(8,9).equals("Q") || nric.substring(8,9).equals("R") || nric.substring(8,9).equals("S") || nric.substring(8,9).equals("T") || nric.substring(8,9).equals("U") || nric.substring(8,9).equals("V") || nric.substring(8,9).equals("W") || nric.substring(8,9).equals("X") || nric.substring(8,9).equals("Y") || nric.substring(8,9).equals("Z"))
    {
    out.println("Ok.");
    }
    else
    {
    out.println("NRIC last char should be a UPPERCASE ALPHABET.");
    }
    }
    else
    {
    out.println("NRIC first char should be UPPERCASE S or F.");
    }
    }
    else
    {
    out.println("NRIC total length not = 9.");
    }
    }
    }
    catch ( NumberFormatException nfe )
    {
    out.println("Middle 7 chars are not DIGITS.");
    }
  16. #9
  17. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    thanks... it works..
    i also need to ask if u know how to check a string to see if it contains salutations such as Mrs, Miss, Mdm, Mr; etc..

    pls help if u know..
  18. #10
  19. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    The String class has a startsWith() function.
  20. #11
  21. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    what if the user enters the salutations at the end of the string or in the middle of the string?? is there smthing like a contain method, whereby it checks if the string contains certain word??
  22. #12
  23. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Yup. Bookmark this link. It's your friend.
    http://java.sun.com/j2se/1.3/docs/api/
    Get familiar with the javadoc, and you'll be able to answer 90% of your own questions.

    As for the function you need:
    http://java.sun.com/j2se/1.3/docs/ap...g.html#indexOf(java.lang.String)

IMN logo majestic logo threadwatch logo seochat tools logo