|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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/a...ex/Pattern.html for instructions. |
|
#3
|
|||
|
|||
|
currently i am using the 1.3.1 api. it does not have regex. so what do i do??
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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... |
|
#6
|
|||
|
|||
|
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" );
}
|
|
#7
|
|||
|
|||
|
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."); } } ____________________________________________________ |
|
#8
|
|||
|
|||
|
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."); } |
|
#9
|
|||
|
|||
|
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.. |
|
#10
|
|||
|
|||
|
The String class has a startsWith() function.
|
|
#11
|
|||
|
|||
|
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??
|
|
#12
|
|||
|
|||
|
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/a...ng.html#indexOf(java.lang.String) |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Check NRIC |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|