Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Telephone No Checking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Telephone No Checking" Watch "Telephone No Checking" New topic

Telephone No Checking

Alex Mann

Joined: Dec 15, 2006
Posts: 4
My tel. no. example should be 016-1234567 or 04-1234567 (as long it got a "-" inside). I already made sure that between the number has "-" but still cannot restrict alphabets to enter in other than number. For now my form still can accept alphabets like "abc-defghtb".

Sir, can help me? or can give me some idea
THANK You very much.
Here is my code:--

public boolean chkInputAdd(){
if (telNo == null) {
setMessage("Please enter the telephone number \n");
return false;

if (validateTelNo()) {
setMessage("The telephone num , should in xxx-xxxxxxx format \n");
return false;
return true;

public boolean validateTelNo() {
Boolean isValid = false;
if (!telNo.matches(".+\\-.+")) {
return true;
return isValid;
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60737

This forum is for questions on JSP. Regular expression questions should be addressed in the Java in General forum to which I have moved this post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41029
Instead of a regexp like ".+\\-.+", try something like "\\d{2,3}-\\d{7}".

Ping & DNS - my free Android networking tools app
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Your number check to those restrictions will work fine for numbers inside most countries, but will fail miserably when international numbers are used.
A fully qualified number would be something along the lines of (+12)(0)(12)-1234567 with the dash being optional, and some people replacing the + with 00 or 00-.
You will also optionally see a space between the 3 and 4 in the last sequence, leading the most elaborate number you have to check to be "(00-12)(0)(12)-123 4567".

Alex Mann

Joined: Dec 15, 2006
Posts: 4
I copy the code and it work sucessfully. For the international number, I will keep it in mind and try it on next time.

You helped me a lot
Thank you, Sir
Don't get me started about those stupid light bulbs.
subject: Telephone No Checking
Similar Threads
Class and Object Problem
Please confirm Code works with certificate
Forcing Input With The Scanner Class
comparing an element in one array with all the elements in another array
Equals Method, greatherThan lessThan methods