我有一个editabe datatable。用户将TEL号码插入TEL列(输入文本框)并单击"确定"按钮后,我需要将其视为以下内容:
输入390239266655
输出 39 02 39 26 66 55
因此,我需要在每个第二个数字后放置一个空间,然后将 放在开始。我该怎么做?
以下是我的验证:
<ui:define name="validation-tag">
<f:validateRegex pattern="[0-9+]*" for="contactPhoneNumber" />
</ui:define>
我们必须写一个转换器,这是解决方案:
@FacesConverter(value = "phoneConverter")
public class PhoneConverter
implements
Converter {
@Override
public Object getAsObject(
FacesContext context,
UIComponent component,
String value) {
String stringToDisplay = null;
//
// if (value != null && !value.equals("") && value.startsWith("+")) {
// stringToDisplay = value.substring(1).trim();
// }
return stringToDisplay;
}
@Override
public String getAsString(
FacesContext context,
UIComponent component,
Object value) {
String stringToDisplay = (String) value;
String resultToDisplay = null;
try {
// if (!stringToDisplay.startsWith("+")) {
if (stringToDisplay != null && !stringToDisplay.equals("")) {
resultToDisplay = getPhoneNumbFormat(stringToDisplay);
}
}
} catch (Exception e) {
log.error("PhoneConverter replaceAll failed!!! object:={} NOT FOUND!!!", stringToDisplay, e);
}
return resultToDisplay;
}
private String getPhoneNumbFormat(
String phoneNumber) {
String resultToDisplay = null;
if (!phoneNumber.startsWith("+")) {
resultToDisplay = "+" + phoneNumber.substring(0, 2).concat(" ") + phoneNumber.substring(2).replaceAll("(.{2})(?!$)", "$1 ");
} else if (!phoneNumber.substring(0, 3).startsWith(" ")) {
resultToDisplay = phoneNumber.substring(0, 3).concat(" ") + phoneNumber.substring(3).replaceAll("(.{2})(?!$)", "$1 ");
}
return resultToDisplay;
}
}