my dog learned polymorphism
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
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: 63866

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] [About Bear] [Books by Bear]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Instead of a regexp like ".+\\-.+", try something like "\\d{2,3}-\\d{7}".
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
I agree. Here's the link:
subject: Telephone No Checking
It's not a secret anymore!