You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Wendy Smoak <ja...@wendysmoak.com> on 2005/06/17 02:46:52 UTC

Formatting a phone number with JSTL?

I'm trying to format a 10 digit number stored as a String in my bean, with
dashes after the 3rd and 6th digit.

   <fmt:formatNumber value="${phone.number}" pattern="????" />

The only examples I can find are dates and currency/amounts.  Is this
possible with JSTL?  Is there documentation that says what is legal to put
in a 'pattern'?

(And further, how expensive is it?  I can do the String manipulation when I
populate the bean if that makes more sense.)

Thanks,
Wendy Smoak


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Formatting a phone number with JSTL?

Posted by Rahul Akolkar <ra...@gmail.com>.
On 6/16/05, Wendy Smoak <ja...@wendysmoak.com> wrote:
> I'm trying to format a 10 digit number stored as a String in my bean, with
> dashes after the 3rd and 6th digit.

Not sure if there is an elegant solution using <fmt:formatNumber>,
since it works off of DecimalFormat, which allows prefixes, suffixes
and grouping separators, but in this case:
1) The dash isn't a suffix/prefix
2) It cannot be a grouping separator since the separation is not uniform.

> Is there documentation that says what is legal to put in a 'pattern'?
>

Look up java.text.DecimalFormat

> (And further, how expensive is it?  I can do the String manipulation when I
> populate the bean if that makes more sense.)

Ofcourse, adding "decoratedNumber" to "phone" is one solution.

-Rahul

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: Formatting a phone number with JSTL?

Posted by Woodchuck <wo...@yahoo.com>.
hi Wendy,

if it helps, i found this javascript that can format phone numbers as
it is being typed into the textfield... (the format is customizable...
your pattern would be "###-###-####")

it works rather well in my opinion.

============= BEGIN JAVASCRIPT FUNCTION =============

/**********************************
jsPhoneObject 
Usage: Object Script to be included as a source file. Contains
functions to format a phone number and check the format.
Created by Jeff Lee on 06/28/2001
Last Modified by Jeff Lee on 8/24/2001
Dependants: None
**********************************/


function jsPhoneObject() {
	var defaultFormat = "(###)###-####";
	var minimumFinalLength = "(###)###-####".length;
	
/**********************************
jsPhone.fixFormat(phone number to be formatted, optional format)
Usage: fixFormat formats a phone 
Example:
<input type="text" onkeyup="this.value=jsPhone.fixFormat(this.value);">
<input type="text"
onblur="this.value=jsPhone.fixFormat(this.value,'###-###-####');">
**********************************/
	this.fixFormat = function (theValue,theFormat){ // this creates a
method in the jsPhoneObject with a minimum of 1 argument
		if(event.keyCode==8) return theValue;
		argv = this.fixFormat.arguments;
		format = ((argv.length==2)?argv[1]:defaultFormat);
		theValue = theValue.replace(/[^0-9]/g,"");
		if(theValue=="") return theValue;
		tmp="";
		j=0;
		for (i=0;i<format.length;i++) {
			if(format.substr(i,1)=="#") {
				tmp+=theValue.substr(j,1);
				j++
				if(j==theValue.length) break;
			} else if(format.substr(i,1)=="%") {
				while (true) {
					tmp+=theValue.substr(j,1);
					j++
					if(j==theValue.length) break;
				}
				break;
			} else {
				tmp+=format.substr(i,1);
			}
		}
		return tmp;
	}
	
	this.checkFormat = function (theValue){
		if(this.checkFormat.arguments.length>1) {
			minimumLength =
((this.checkFormat.arguments.length>2)?this.checkFormat.arguments[2]:this.checkFormat.arguments[1].length);
			if(theValue.length < minimumLength) return false;
			if(theValue!=this.fixFormat(theValue,this.checkFormat.arguments[1]))
return false;
		} else {
			if(theValue.length < minimumFinalLength) return false;
			if(theValue!=this.fixFormat(theValue)) return false;
		}
		return true;
	}
}

var jsPhone = new jsPhoneObject();

============= END JAVASCRIPT FUNCTION =============


woodchuck





--- Wendy Smoak <ja...@wendysmoak.com> wrote:

> I'm trying to format a 10 digit number stored as a String in my bean,
> with
> dashes after the 3rd and 6th digit.
> 
>    <fmt:formatNumber value="${phone.number}" pattern="????" />
> 
> The only examples I can find are dates and currency/amounts.  Is this
> possible with JSTL?  Is there documentation that says what is legal
> to put
> in a 'pattern'?
> 
> (And further, how expensive is it?  I can do the String manipulation
> when I
> populate the bean if that makes more sense.)
> 
> Thanks,
> Wendy Smoak
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> 
> 



		
____________________________________________________ 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org