Results 1 to 15 of 15
Thread: Help!! Validation for java
- 01-07-2011, 08:29 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
- 01-07-2011, 08:34 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
No codes; you have to do that yourself. A telephone number is a sequence of digits but it isn't a number, e.g. 0317153 ... start with a zero, no actual number does but a telephone number can. Keep the telephone number as a String (that only contains digits). So basically you have to test a String for containing just digits (at least one digit?).
There are several ways to do it, starting from a simple loop that checks each character in the String (also read the API documentation of the Character class) ending with regular expressions that can do the entire job for you.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-07-2011, 08:51 AM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
you can use regular expression.
say your phone number is stored in a a String phoneNumber.
"^\\d+$" means match the string starting from the beginning, find one or more digits till the end of the line. There are also other boundary matchers like \A , \z etc you can take a look at the Pattern class documentation. Of course, the above is simplistic case of a phone number. Depending on what your requirements are , the regex will be more that just checking for all digits.Java Code:import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexDemo { public static void main(String[] args){ String phoneNumber = "1234567"; Pattern ex = Pattern.compile("^\\d+$"); Matcher match = ex.matcher(phoneNumber); while (match.find() ){ System.out.println( match.group() ); } } }
- 01-07-2011, 09:17 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Spoonfeeding reported again; you're not helping the OP in any way like this.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-07-2011, 09:27 AM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
Last edited by JavaHater; 01-07-2011 at 09:34 AM.
- 01-07-2011, 09:47 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 10
- Rep Power
- 0
i think, validating these fields needs to be one at a time if possible.
for example you have the following fields for a student user:
id number
name
address
birthdate
telephone number
in order for you to validate these items, you should have at least a list of data or database for you to compare with.
validation is not limited to this. it also have something to do with how correct the input is.
a good example would be the telephone number. there are a lot of telephone number formats such us the following:
(032)458-5824
0922555444
just like josah said, you have to make it a string and through that you have to check the area of the user through his address and the area code he uses in his number.
another one is also the birthdate... if you have a user with a birthdate in 1552, can he still be a student now? for sure no...
you have to be logical and intelligent enough to think of a strategy how to validate these fields... your way of thinking would direct you what codes to key in... :)
i hope this would help you in anyway...
wahehehe . .. :)
- 01-07-2011, 09:49 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 01-07-2011, 09:51 AM #8
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
- 01-07-2011, 09:54 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 01-07-2011, 09:56 AM #10
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
- 01-07-2011, 09:58 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 01-07-2011, 10:08 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 01-07-2011, 10:14 AM #13
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
- 01-07-2011, 10:19 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
If it's a test I suspect simply confirming it's numbers is going to be more than enough. If it's the real world then you are quite correct. We don't know though.
The point is, they asked for code without providing any themselves. The general technique is to get them to actually do some work first to at least show willing on their part then start providing help. Throwing code at code requests is rarely a good idea.
- 01-07-2011, 10:33 AM #15
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
that's not true right? I provide examples, while you don't. so who's the one with no value?
there is always 2 sides to a coinyou're rude and impolite,
you are egotistical in your remark. You should be the one to be banned.your Java knowledge is mediocre at best and you should be banned from here asap.
Similar Threads
-
Live Email Validation In Java
By shameel in forum Web FrameworksReplies: 9Last Post: 01-05-2010, 05:42 AM -
File path validation in Java
By aks123 in forum Advanced JavaReplies: 2Last Post: 10-07-2009, 04:38 PM -
Java Form Validation
By Lovie in forum Java ServletReplies: 7Last Post: 09-22-2009, 05:52 PM -
VAlidation
By chetna1982 in forum New To JavaReplies: 2Last Post: 12-11-2008, 07:08 AM -
java validation?
By lockmac in forum New To JavaReplies: 3Last Post: 08-14-2007, 04:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks