Supar Man
Supar Man

Reputation: 59

JSTL - formatNumber : how to show phone number in (###) ####### format

I am trying to format phone number in (###) ####### format. I am doing like that ..

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:formatNumber type="number" pattern="(###) #######" value="${phoneNumber}" />

but it is not converting is the correct format. Showing value like that ..

Output

(9000000000)

It should show me like that .. (900) 0000000

I am putting normal 10 digit phone number. It should show me in proper format in front end. Help me

Upvotes: 1

Views: 7505

Answers (2)

Gabriel Xunqueira
Gabriel Xunqueira

Reputation: 433

I have also needed this. It seems you cannot do that with fmt:formatNumber, which is only for pure numbers: lets you put decimal points and thousands separators and so on. What we want to do here is really to parse a string and to format it in a way, so I guess Braj's is the way. You could also write your own tag, of course.

In this case this may be an overkill, but it may not if you need to do this all around or the tag library is already created.

public class FormatTelTag extends TagSupport {
private String tel;
public FormatTelTag() { }

public int doStartTag() throws JspTagException {
    if (tel != null && !tel.isEmpty()) {
        JspWriter out = pageContext.getOut();
        try {
            out.write(formatTelephone(tel));
        } catch (IOException e) {
            throw new JspTagException(e.getMessage(), e);
        }
    }
    return EVAL_PAGE;
}

public static String formatTelephone(final String tel) {
    String formatted = null;
    if (tel == null || tel.isEmpty()) {
        formatted = tel;    
    } else {
        formatted = "";
        for (char c : tel.toCharArray()) {
            if (Character.isDigit(c)) {
                formatted = formatted + c;
                if (formatted.length() == 3 || formatted.length() == 7) {
                    formatted = formatted + "-";
                }
            }
        }
    }
    return formatted;
}

public String getTel() {
    return tel;
}
public void setTel(String tel) {
    this.tel = tel;
}

}

This would be the library descriptor cus.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
    <description>Custom tags</description>
    <tlib-version>1.0</tlib-version>
    <short-name>cus</short-name>
    <tag>
        <name>formatTel</name>
        <tag-class>myPackage.FormatTelTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <description>A telephone</description>
            <name>tel</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</taglib>

Which placed under web-inf would be usable like

<%@ taglib prefix="cus" uri="/WEB-INF/cus"%>
...
<cus:formatTel tel="1234098766" />

I also needed to map this URI in web.xml

Upvotes: 1

Braj
Braj

Reputation: 46841

Let's try it using fn:substring and fn:length.

<c:set value="9123456789" var="phone"/>
<c:out value="(${fn:substring(phone, 0, 4)}) ${fn:substring(phone, 4, fn:length(phone))}"/>

output:

(9123) 456789 

Upvotes: 3

Related Questions