Results 1 to 15 of 15
  1. #1
    peppy99 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Smile Help!! Validation for java

    Guys i'm doing my java work and stuck at validation part.
    It's to check that the user enters correct information. Like to input their phone number at the telephone fields.

    Can any of you help me with the codes?
    thanks.:)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by peppy99 View Post
    Guys i'm doing my java work and stuck at validation part.
    It's to check that the user enters correct information. Like to input their phone number at the telephone fields.

    Can any of you help me with the codes?
    thanks.:)
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by peppy99 View Post
    Guys i'm doing my java work and stuck at validation part.
    It's to check that the user enters correct information. Like to input their phone number at the telephone fields.

    Can any of you help me with the codes?
    thanks.:)
    you can use regular expression.

    say your phone number is stored in a a String phoneNumber.
    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() );
            }
        }
    }
    "^\\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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Spoonfeeding reported again; you're not helping the OP in any way like this.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    Spoonfeeding reported again; you're not helping the OP in any way like this.

    Jos
    Am i giving the whole solution? NO.
    This is purely an example, one you can see even in the docs. Heck, the docs has way more elaborate examples than mine.
    Shall I report you as a troll?

    Edit:
    Heck , why should i even hesitate
    You are reported as a troll
    Last edited by JavaHater; 01-07-2011 at 09:34 AM.

  6. #6
    kimhonoridez is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    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 . .. :)

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,962
    Rep Power
    19

    Default

    Quote Originally Posted by JavaHater View Post
    Am i giving the whole solution? NO.
    This is purely an example, one you can see even in the docs. Heck, the docs has way more elaborate examples than mine.
    Shall I report you as a troll?

    Edit:
    Heck , why should i even hesitate
    You are reported as a troll
    Then point them at the docs, since learning to RTFM is one skill they ought to pick up.

  8. #8
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Tolls View Post
    Then point them at the docs, since learning to RTFM is one skill they ought to pick up.
    come on, you are another one who doesn't read properly. I pointed OP to the regex tutorial haven't I ? why don't you tell me what's so wrong with my example?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    come on, you are another one who doesn't read properly. I pointed OP to the regex tutorial haven't I ? why don't you tell me what's so wrong with my example?
    Whatever speeds up your banning is fine with me.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    Whatever speeds up your banning is fine with me.

    Jos
    anyone who has eyes to see and the mind to think will see that my example is only trivial and solves nothing for the OP. You should be banned before me for trolling.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    anyone who has eyes to see and the mind to think will see that my example is only trivial and solves nothing for the OP. You should be banned before me for trolling.
    You add no positive value to this forum, you're rude and impolite, your Java knowledge is mediocre at best and you should be banned from here asap.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,962
    Rep Power
    19

    Default

    Quote Originally Posted by JavaHater View Post
    come on, you are another one who doesn't read properly. I pointed OP to the regex tutorial haven't I ? why don't you tell me what's so wrong with my example?
    Except when you hand out code that's good enough to pass the test then they will not RTFM. That link will be skipped and the code will be pasted in as sufficient.

  13. #13
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Tolls View Post
    Except when you hand out code that's good enough to pass the test then they will not RTFM. That link will be skipped and the code will be pasted in as sufficient.
    wrong. That example will not help OP pass the test. Phone numbers are not just random digits. The OP has to do a lot more than that I have provided. Then he/she will have to RTFM. Got that?

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,962
    Rep Power
    19

    Default

    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.

  15. #15
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    You add no positive value to this forum,
    that's not true right? I provide examples, while you don't. so who's the one with no value?

    you're rude and impolite,
    there is always 2 sides to a coin

    your Java knowledge is mediocre at best and you should be banned from here asap.
    you are egotistical in your remark. You should be the one to be banned.

Similar Threads

  1. Live Email Validation In Java
    By shameel in forum Web Frameworks
    Replies: 9
    Last Post: 01-05-2010, 05:42 AM
  2. File path validation in Java
    By aks123 in forum Advanced Java
    Replies: 2
    Last Post: 10-07-2009, 04:38 PM
  3. Java Form Validation
    By Lovie in forum Java Servlet
    Replies: 7
    Last Post: 09-22-2009, 05:52 PM
  4. VAlidation
    By chetna1982 in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 07:08 AM
  5. java validation?
    By lockmac in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •