| Author |
determining if a variable is a number
|
Bernard Sigmund Gustav
Ranch Hand
Joined: Dec 20, 2005
Posts: 170
|
|
Hello everyone. I hope you can help me with this. I have my index.jsp which is basically a login page. It passes the values to login.jsp which authenticates. Now I need to trap the inputted values in the username since it has to be 4 digits. what i do is to get the username and convert it to a integer since it gets a string as in this: String uname = request.getParameter("username"); int badge = Integer.parseInt(uname); but if i input characters instead of strings in my username it errors since it cant parse. so i need to determine first if the input contains the digits 0-9 for it to be valid and invalid if it has otherwise. thanks in advance for the help
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56150
|
|
|
This is independent of JSP so I've moved it to Java in General (intermediate).
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
|
|
|
Why not just catch the NumberFormatException? Or parse it with a regular expression and see if the entire thing matches. Or iterate over each character checking for a digit.
|
 |
Jherald Lacambra
Ranch Hand
Joined: Feb 02, 2005
Posts: 129
|
|
<input type="text" name="username" on blur="validateUserName(this)"> <script language="javascript"> function validateUserName(field){ if(isNaN(field.value)){ field.value=""; alert("Please enter a correct id") } } </script>
|
jherald
|
 |
Bernard Sigmund Gustav
Ranch Hand
Joined: Dec 20, 2005
Posts: 170
|
|
i don't want to use javascript, but thanks i just converted the string to an array of characters and then iterated over each character and used the isDigit method. thanks
|
 |
Rusty Shackleford
Ranch Hand
Joined: Jan 03, 2006
Posts: 490
|
|
As was sort of stated, Use Integer.parseInt to check if it is a integer. If it isn't it will throw a NumberFormatException and you can easily handle the error. To check if it is of length 4, just use the length() method in String. It is far easier, more efficient and less error prone to do it this way then coverting it to an array and futzing with that. The javascript suggestion is a good one, but use it with caution. It will make it faster and easier for the user to fix mistakes since it is all done client side. Also if errors are caught server side it is a bit less work for your server, since it won't have to generate another page to tell the user of the error along with the form. BUT, there are no guarantees that data coming to your server used the page you wrote, or that Javascript is enabled. So you still have to have data checking server-side(unless you are insane), but it is still a good idea. Relying on javascript for your sole method of data checking may not only result in bad data, but is a huge security risk. Just something to consider. [ February 15, 2006: Message edited by: Rusty Shackleford ]
|
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
To see if a string is a valid int, I'd certainly use the parseInt() and exception technique. Valid ints are just tricky enough that I'd probably get it wrong on some boundary condition or oddball syntax. To see if something is four digits between 0000 and 9999 a quick regular expression match for exactly four digits would be short and meaningful to the reader. If you've never used RegEx this might be an excuse to dip your toe in the water. See Pattern in the JavaDoc.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: determining if a variable is a number
|
|
|