function HaveGB2312(const temp:string):boolean;
var
s:string;
w:widestring;
begin
w:=temp;
s:=temp;
if length(w)=length(s) then //这样可以判断出该字符中是否包含双字节的字符
result:=false
else
result:=true;
end;
begin
a:=edit1.text;
for i:=1 to length(a) do
begin
if (i mod 2 <> 0) and (ord(a[i])>128) then continue; // 每个汉字占2字节,且汉字的第一个字节的ASCII码>128,而英文<128
if i mod 2=0 then continue;
end;
if i<>length(a) then ERROR;//如果不是正常结束循环则发出错误信息
end;
begin
a:=edit1.text;
for i:=1 to length(a) do
begin
if (i mod 2 <> 0) and (ord(a[i])>128) then continue; // 每个汉字占2字节,且汉字的第一个字节的ASCII码>128,而英文<128
if i mod 2=0 then continue;
end;
if i<>length(a) then ERROR;//如果不是正常结束循环则发出错误信息
end;
不用判断ASCII码,这样就行了:
在确定的按钮click事件中写:
var
WS:WideString;
begin
WS:=Edit1.Text;
if Odd(Length(Edit1.Text)) or (Length(WS)<>(Length(Edit1.Text) div 2) ) then
showMessage('必须输入中文');
end;