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.:)
Printable View
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
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.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() );
}
}
}
Spoonfeeding reported again; you're not helping the OP in any way like this.
Jos
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 . .. :)
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.
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 coinQuote:
you're rude and impolite,
you are egotistical in your remark. You should be the one to be banned.Quote:
your Java knowledge is mediocre at best and you should be banned from here asap.