业务需求书上要求:用途,备注(附言)栏输入的字符,系统要控制非法字符。包括但不限于符合GB2132编码规定;对X字符集的“℅、:、”等字符进行录入控制,
若录入非法字符时显示提示。我的理解是:支票用途,汇款备注(附言)输入栏,可以输入 ! % 《》 { } ( ); ^_^ 当然中文和英文是可以的啦。
我的实现代码如下 ,但不能完全实现这个功能,请各位牛人来协助解决。
若录入非法字符时显示提示。我的理解是:支票用途,汇款备注(附言)输入栏,可以输入 ! % 《》 { } ( ); ^_^ 当然中文和英文是可以的啦。
我的实现代码如下 ,但不能完全实现这个功能,请各位牛人来协助解决。
private bool submitValidate()
{
//校验用途是否符合G字符集
if (!ClientValidationHelper.GB2312CharSetCheck(this.bos_ct_purpose.Text))
{
MessageInfoHelper.Instance.ShowMessage("用途输入格式非法!不允许X字符集的“%、:、”等字符");
return false;
}
if (!this.Validate(this.data))
{
return false;
}
if (this.bos_ct_purpose.GetValue().ToString() == "")
{
MessageInfoHelper.Instance.ShowErrorMessage("请输入业务要素!");
return false;
}
return true;
}
/// 检查数据是否符合GB2312标准
/// <param name="gb2312String"></param>
public static bool GB2312CharSetCheck(string gb2312String)
{
if (gb2312String == null)
return false;
gb2312String = gb2312String.Trim();
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(gb2312String);
return GB2312CharSetCheck(bytes);
}
/// 检查数据是否符合GB2312标准
/// <param name="gb2312bytes"></param>
public static bool GB2312CharSetCheck(byte[] gb2312bytes)
{
if (null == gb2312bytes)
return false;
for (int i = 0; i < gb2312bytes.Length - 1; i++)
{
int current = Convert.ToInt32(gb2312bytes[i]);
int next = Convert.ToInt32(gb2312bytes[i + 1]);
if ((current >= 0x81) && (current <= 0xFE) && (next >= 0x40)
&& (current != 0x7F) && (current != 0x7B) && (current != 0x7D) && (next <= 0xFE))
{
continue;
}
else
return false;
}
return true;
}