1先来看看正则表达式的基本语法:
1.1普通字符:字母、数字、汉字、下划线以及没有被定义特殊意义的标点符号
1.2简单的转义字符:一些不便书写的字符,比如换行符,制表符等,使用 \n,\t 来表示。另外有一些标点符号在正则表达式中,被定义了特殊的意义,因此需要在前面加 "\" 进行转义后,匹配该字符本身。
1.2.1简单列举一下常用的特殊意义的转义符,方便查看
^ 匹配输入字符串的开始位置,如匹配本身则加转义符\ ,即\^(下同)
$ 匹配输入字符串的结束位置
() 标记一个子字符串的开始和结束位置
[] 用来自定义能够匹配"多种字符"的表达式
{} 修饰匹配的次数
. 修饰匹配除了换行符(\n)以外的任意一个字符
? 修饰匹配次数为 0 次或 1 次
+ 修饰匹配次数为至少 1 次
* 修饰匹配次数为 0 次或任意次
| 左右两边表达式之间 "或" 关系
1.2.2 以上特殊意义的转义符具有的共同特征是:匹配本身均为加上转义符"\"
1.2.3 常用标准字符表达集合
. 小数点可以匹配除了换行符(\n)以外的任意一个字符
\w 可以匹配任何一个字母或者数字或者下划线(注w小写)
\W 可以匹配任何一个字母或者数字或者下划线以外的字符(注W大写)
\d 可以匹配0-9中的任意一个数字字符
\D D大写可以匹配任何一个非数字字符
1.2.4 自定义字符集合
用中括号 [ ] 包含多个字符,可以匹配所包含的字符中的任意一个。同样,每次只能匹配其中一个
用中括号 [^ ] 包含多个字符,构成否定格式,可以匹配所包含的字符之外的任意一个字符。
注:
正则表达式中的特殊符号,如果被包含于中括号中,则失去特殊意义,但 \ [ ] : ^ - 除外。
标准字符集合,除小数点(.)外,如果被包含于中括号中,自定义字符集合将包含该集合。
比如:[\d.\-+],将可以匹配数字,小数点和 + - 符号。(小数点和 + 号失去特殊意义)
用减号相连的 2 个普通字符,自定义字符集合将包含该范围。
比如:[\dA-Fa-f],将可以匹配 0 - 9, A - F, a - f
2下面结合GWT-EXT的NumberField实现提示信息的自定义
定义全局变量
- // 单价
- private NumberField unitPrice;
- // 工艺费
- private NumberField craftFee;
- // 石重量
- private NumberField stoneWeight;
- // 总重
- private NumberField totalWeight;
// 单价
private NumberField unitPrice;
// 工艺费
private NumberField craftFee;
// 石重量
private NumberField stoneWeight;
// 总重
private NumberField totalWeight;
初始化
- // 石重量
- this.stoneWeight = new NumberField();
- this.stoneWeight.setFieldLabel("石重(克拉)");
- this.stoneWeight.setMaxLength(10);
- //响应光标离开输入框时的事件
- this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
- public void handleEvent(BaseEvent be) {
- setFormat(stoneWeight, stoneWeight.getRawValue());
- }
- }); // 单价
- this.unitPrice = new NumberField();
- this.unitPrice.setFieldLabel("<font color='red'>*</font>单价");
- this.unitPrice.setAllowDecimals(true);
- this.unitPrice.setAllowBlank(false);
- this.unitPrice.setMaxLength(10);
- this.unitPrice.setFormat(NumberFormat.getDecimalFormat());
- this.unitPrice.addListener(Events.OnBlur, new Listener<BaseEvent>() {
- public void handleEvent(BaseEvent be) {
- autoSetFormat(unitPrice, unitPrice.getRawValue());
- }
- });
- // 工艺费
- this.craftFee = new NumberField();
- this.craftFee.setFieldLabel("<font color='red'>*</font>工艺费");
- this.craftFee.setAllowDecimals(true);
- this.craftFee.setAllowBlank(false);
- this.craftFee.setMaxLength(10);
- this.craftFee.setFormat(NumberFormat.getDecimalFormat());
- this.craftFee.addListener(Events.OnBlur, new Listener<BaseEvent>() {
- public void handleEvent(BaseEvent be) {
- autoSetFormat(craftFee, craftFee.getRawValue());
- }
- });
- // 石重量
- this.stoneWeight = new NumberField();
- this.stoneWeight.setFieldLabel("石重(克拉)");
- this.stoneWeight.setMaxLength(10);
- this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
- public void handleEvent(BaseEvent be) {
- setFormat(stoneWeight, stoneWeight.getRawValue());
- }
- });
- // 总重
- this.totalWeight = new NumberField();
- this.totalWeight.setFieldLabel("总重(克)");
- this.totalWeight.setMaxLength(10);
- this.totalWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
- public void handleEvent(BaseEvent be) {
- setFormat(totalWeight, totalWeight.getRawValue());
- }
- });
// 石重量
this.stoneWeight = new NumberField();
this.stoneWeight.setFieldLabel("石重(克拉)");
this.stoneWeight.setMaxLength(10);
//响应光标离开输入框时的事件
this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
public void handleEvent(BaseEvent be) {
setFormat(stoneWeight, stoneWeight.getRawValue());
}
}); // 单价
this.unitPrice = new NumberField();
this.unitPrice.setFieldLabel("<font color='red'>*</font>单价");
this.unitPrice.setAllowDecimals(true);
this.unitPrice.setAllowBlank(false);
this.unitPrice.setMaxLength(10);
this.unitPrice.setFormat(NumberFormat.getDecimalFormat());
this.unitPrice.addListener(Events.OnBlur, new Listener<BaseEvent>() {
public void handleEvent(BaseEvent be) {
autoSetFormat(unitPrice, unitPrice.getRawValue());
}
});
// 工艺费
this.craftFee = new NumberField();
this.craftFee.setFieldLabel("<font color='red'>*</font>工艺费");
this.craftFee.setAllowDecimals(true);
this.craftFee.setAllowBlank(false);
this.craftFee.setMaxLength(10);
this.craftFee.setFormat(NumberFormat.getDecimalFormat());
this.craftFee.addListener(Events.OnBlur, new Listener<BaseEvent>() {
public void handleEvent(BaseEvent be) {
autoSetFormat(craftFee, craftFee.getRawValue());
}
});
// 石重量
this.stoneWeight = new NumberField();
this.stoneWeight.setFieldLabel("石重(克拉)");
this.stoneWeight.setMaxLength(10);
this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
public void handleEvent(BaseEvent be) {
setFormat(stoneWeight, stoneWeight.getRawValue());
}
});
// 总重
this.totalWeight = new NumberField();
this.totalWeight.setFieldLabel("总重(克)");
this.totalWeight.setMaxLength(10);
this.totalWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
public void handleEvent(BaseEvent be) {
setFormat(totalWeight, totalWeight.getRawValue());
}
});
响应事件的方法
- /**
- * 自动设置数字的格式
- * 如输入的为整数则自动加上两位小数
- * 如输入的带有小数则验证是否为两位小数,不是则报错
- * @param f为NumberField
- * @param value输入值
- */
- private static void autoSetFormat(NumberField f, String value) {
- if (!value.equals("")) {
- if (value.indexOf(".") == -1) {
- value += ".00";
- f.setRawValue(value);
- f.clearInvalid();
- } else {
- f.setRegex("^[\\d]*[\\.][\\d][\\d]$");
- f.getMessages().setRegexText("小数点后保留两位");
- }
- }
- }
- /**
- * 自动设置数字的格式
- * 如输入的为整数则自动加上三位小数
- * 如输入的带有小数则验证是否为三位小数,不是则报错
- * @param f为NumberField
- * @param value输入值
- */
- private static void setFormat(NumberField f, String value) {
- if (!value.equals("")) {
- if (value.indexOf(".") == -1) {
- value += ".000";
- f.setRawValue(value);
- f.clearInvalid();
- } else {
- f.setRegex("^[\\d]*[\\.][\\d]{3}$");
- f.getMessages().setRegexText("小数点后保留三位");
- }
- }
- }
/**
* 自动设置数字的格式
* 如输入的为整数则自动加上两位小数
* 如输入的带有小数则验证是否为两位小数,不是则报错
* @param f为NumberField
* @param value输入值
*/
private static void autoSetFormat(NumberField f, String value) {
if (!value.equals("")) {
if (value.indexOf(".") == -1) {
value += ".00";
f.setRawValue(value);
f.clearInvalid();
} else {
f.setRegex("^[\\d]*[\\.][\\d][\\d]$");
f.getMessages().setRegexText("小数点后保留两位");
}
}
}
/**
* 自动设置数字的格式
* 如输入的为整数则自动加上三位小数
* 如输入的带有小数则验证是否为三位小数,不是则报错
* @param f为NumberField
* @param value输入值
*/
private static void setFormat(NumberField f, String value) {
if (!value.equals("")) {
if (value.indexOf(".") == -1) {
value += ".000";
f.setRawValue(value);
f.clearInvalid();
} else {
f.setRegex("^[\\d]*[\\.][\\d]{3}$");
f.getMessages().setRegexText("小数点后保留三位");
}
}
}
效果图在附件中
附
电子邮件
^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$
汉字(限定只能输入m-n个汉字,m,n值可以修改)
^[\u4e00-\u9fa5]{m,n}$