salesforce入力规则中检测全角汉字
参考下面的URL就可以了
本人写的程序如下
- 入力规则:
NOT (REGEX(Name, '^[0-9A-Za-zァ-ヴぁ-ん亜-熙]*$'))
- CommonUtil.cls
public class CommonUtils {
public CommonUtils() {
}
public static Boolean isFullCharacter(String chkString) {
if (String.isBlank(chkString)) {
return true;
}
// Pattern p = Pattern.compile('^[0-9A-Za-zァ-ヴぁ-ヶ亜-熙、。]*$');
// NOT (REGEX(Name, '^[0-9A-Za-zァ-ヴぁ-ん亜-熙]*$'))
Pattern p = Pattern.compile('^[0-9A-Za-zァ-ヴぁ-ん亜-熙]*$');
Matcher m = p.matcher(chkString);
if (m.matches()) {
return true;
} else {
return false;
}
}
}
- 测试
@istest
public with sharing class CommonUtilsTest {
public CommonUtilsTest() {
}
@istest
private static void isFullCharacterTest001(){
// 全角
System.assert(CommonUtils.isFullCharacter('123234234'));
System.assert(CommonUtils.isFullCharacter('abcedfg'));
System.assert(CommonUtils.isFullCharacter('ABCEDFG'));
System.assert(CommonUtils.isFullCharacter('アイウエオ'));
System.assert(CommonUtils.isFullCharacter('あいうえられて'));
System.assert(CommonUtils.isFullCharacter('あいABCEDアイウエオFGうabcedfgえ123234234られて'));
System.assert(CommonUtils.isFullCharacter('成城石全角文字'));
System.assert(CommonUtils.isFullCharacter('成城アイウエ1いABCEDアイウエオFGうabcedfgえ123223234234石え全角文字'));
// 半角
System.assert(!CommonUtils.isFullCharacter('アイウエオバビブベボパピプペポ'));
System.assert(!CommonUtils.isFullCharacter('asdasdda'));
System.assert(!CommonUtils.isFullCharacter('231eeee32123'));
// 混在
System.assert(!CommonUtils.isFullCharacter('12343241231232323'));
System.assert(!CommonUtils.isFullCharacter('12345336722890'));
System.assert(!CommonUtils.isFullCharacter('アイウエオアイウエオバビブベボパピプペポ'));
}
}
注意点:
日语中有第一水准和第二水准汉字之分,
其次还有各种非标准全角字符
需要依据式样定义,添加。