本文章也是针对于asp教程.net来做的,就是关于asp.net教程 c#与vb.net 只能输入英文和数字函数,第一款是c#限制只能输入数字与英文,后一示是vb.net做的,原理都一个个字符然后判断他的内码值如下实例。
c#
private void textbox1_textchanged(object sender, eventargs e)
{
char[] clist = textbox1.text.tochararray();
string newstr = "";
for (int i = 0; i < clist.length; i++)
{
int ascii = (int)clist[i];
if (ascii > 127)
{
clist[i] = '';
}
newstr += clist[i].tostring();
}textbox1.text = newstr;
}
vb.net
function valitext(keyin as integer, validatestring as string, editable as boolean) as integer
dim validatelist as string
dim keyout as integer
if editable = true then
validatelist = ucase(validatestring) & chr(8)
else
validatelist = ucase(validatestring)
end if
if instr(1, validatelist, ucase(chr(keyin)), 1) > 0 then
keyout = keyin
else
keyout = 0
end if
valitext = keyout
end function
调用方法
private sub txtmoney_keypress(keyascii as integer)
keyascii = valitext(keyascii, "0123456789.", true)
end sub
private sub txtid_keypress(keyascii as integer)
keyascii = valitext(keyascii, "0123456789-abcdefghijklmnopqrstuvwxyz", true)
end sub