rt
解决方案 »
- 利用VB修改数据库中的密码
- RDS服务,远程连接Access数据库,服务器和客户端分别在不同网段可以吗??
- 远程数据库访问的问题!
- 你有兴趣吗?
- 多彩龙企业综合管理系统-仓库演示新春第一版 8.0已经发布
- 关于MSFlexGrid的一个简单问题!在线等
- 急!!!!VB如何实现让windows2000休眠???马上给分!!!!!
- 请问那里有关于mts(transaction server)的书籍介绍,中文。倾分奉献!!
- 在程序中怎样调用剪贴板事件?
- 请教高手:100分求解100万条记录的记录集处理方法
- VB下的3D如何做:
- 【终于回到老窝了】哎,不容易呀,一路上被查包..........我真长得像个KB分子?
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End Sub
select case keyascii
case 48 to 57 '数字
case 8 '退格键
case else
keyascii=0
end select
End Sub
If KeyAscii <> 8 And KeyAscii <> 127 Then
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End If
End Sub
private sub text1_lostfocus()
if trim(text1.text)<>"" then
if not isnumeric(text1.text) then
msgbox "请输入数值型数据!",48,"提示"
text1.text=""
text1.setfocus
end if
end if
end sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim i As Integer
Dim j As Integer
If KeyAscii > 57 Or KeyAscii < 48 And KeyAscii <> 46 And KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 Then
Text1.Locked = True
ElseIf KeyAscii = 46 Then
i = InStr(Text1.Text, ".")
j = Len(Trim(Text1.Text))
If j = 0 Then
Text1.Locked = True
ElseIf i = 0 Then
Text1.Locked = False
Else
Text1.Locked = True
End If
ElseIf KeyAscii = 45 Then
j = Len(Trim(Text1.Text))
If j = 0 Then
Text1.Locked = False
Else
Text1.Locked = True
End If
Else
Text1.Locked = False
End If
End Sub
Dim ret As Long
Const GWL_STYLE = (-16)
Const ES_NUMBER = &H2000
lOldStytle = GetWindowLong(Text1.hWnd, GWL_STYLE)
lOldStytle = lOldStytle Or ES_NUMBER
ret = SetWindowLong(Text1.hWnd, GWL_STYLE, lOldStytle)
正解,赞一个。我补一个完整版本吧:Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE As Long = -16
Private Const ES_NUMBER As Long = &H2000&Private Sub Form_Load()
Dim lOldStytle As Long
Dim ret As Long lOldStytle = GetWindowLong(Text1.hwnd, GWL_STYLE)
lOldStytle = lOldStytle Or ES_NUMBER
ret = SetWindowLong(Text1.hwnd, GWL_STYLE, lOldStytle)
End Sub
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> Asc(".") And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
补充一点,不能输入两个小数点
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> Asc(".") And KeyAscii <> 8 Then
KeyAscii = 0
End If
If KeyAscii = Asc(".") And InStr(Text1.Text, ".") > 0 and leThen
KeyAscii = 0
End If
End Sub
'我的想法是,在改变内容的时间进行设置.
'如果改变后还是数值型,就保存起来.
'如果改变后不是数值型,就让他改为前一次的保存结果.Option ExplicitPrivate OldText As DoublePrivate Sub Text1_Change()
If IsNumeric(Text1.Text) Then
OldText = Text1.Text
Text1.SelStart = Len(Text1.Text)
Else
Text1.Text = OldText
Text1.SelStart = Len(Text1.Text)
End If
End Sub
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
这样也行
onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;"
id="txtY_Revenue" style="TEXT-ALIGN: right" runat="server" Width="90%" MaxLength="12">
</asp:textbox>
说明: 此方法控制TextBox只收数字:0~9 , 也自可以定义其它可输入字符,如改成: 65~123,只允许输入: a~z和A~Z 等.
{
e.Handled = !(char.IsDigit(e.KeyChar) ¦ ¦(e.KeyChar == 8) ¦ ¦(e.KeyChar == 45) ¦ ¦(e.KeyChar ==46));
}上面是我在另外一个帖子的回复
http://topic.csdn.net/u/20080805/14/6583cd12-8e2a-4a09-a1f5-acc2236d5345.html
我19楼的回复是.NET的解决方案
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<script type="text/javascript">
//正整数的验证
function valNum(ev)
{
if(window.event.shiftKey)
{
ev.returnValue = "";
}
else
{
var e = ev.keyCode;
//允许的有大、小键盘的数字,左右键,backspace, delete, Control + C, Control + V
if(e != 48 && e != 49 && e != 50 && e != 51 && e != 52 && e != 53 && e != 54 && e != 55 && e != 56 && e != 57 && e != 96 && e != 97 && e != 98 && e != 99 && e != 100 && e != 101 && e != 102 && e != 103 && e != 104 && e != 105 && e != 37 && e != 39 && e != 13 && e != 8 && e != 46 && e != 9)
{
if(ev.ctrlKey == false)
{
//不允许的就清空!
ev.returnValue = "";
}
else
{
//验证剪贴板里的内容是否为数字!
valClip(ev);
}
}
}
}
//验证剪贴板里的内容是否为数字!
function valClip(ev)
{
//查看剪贴板的内容!
try
{
var content = clipboardData.getData("Text");
if(content != null)
{
var test = parseInt(content);
var str = "" + test;
if(isNaN(test) == true)
{
//如果不是数字将内容清空!
clipboardData.setData("Text","");
}
else
{
if(str != content)
clipboardData.setData("Text", str);
}
}
}
catch(e)
{
//清空出现错误的提示!
alert("粘贴出现错误!");
}
}
//验证浮点数
function valFloat(ev,thisValue)
{
if(window.event.shiftKey==false)
{
if(thisValue!="")
{
var e = ev.keyCode;
var re=/\./g;
var arr;
if((arr = re.exec(thisValue)) == null)
{
//允许的有大、小键盘的数字,左右键,backspace, delete, Control + C, Control + V,小数点
if(e!=190 && e!=110&& e != 48 && e != 49 && e != 50 && e != 51 && e != 52 && e != 53 && e != 54 && e != 55 && e != 56 && e != 57 && e != 96 && e != 97 && e != 98 && e != 99 && e != 100 && e != 101 && e != 102 && e != 103 && e != 104 && e != 105 && e != 37 && e != 39 && e != 13 && e != 8 && e != 46 && e != 9)
{
if(ev.ctrlKey == false)
{
//不允许的就清空!
ev.returnValue = "";
}
else
{
//验证剪贴板里的内容是否为浮点数!
valFloatClip(ev);
}
}
}
else
{
//允许的有大、小键盘的数字,左右键,backspace, delete, Control + C, Control + V,小数点
if( e != 48 && e != 49 && e != 50 && e != 51 && e != 52 && e != 53 && e != 54 && e != 55 && e != 56 && e != 57 && e != 96 && e != 97 && e != 98 && e != 99 && e != 100 && e != 101 && e != 102 && e != 103 && e != 104 && e != 105 && e != 37 && e != 39 && e != 13 && e != 8 && e != 46 && e != 9)
{
if(ev.ctrlKey == false)
{
//不允许的就清空!
ev.returnValue = "";
}
else
{
//验证剪贴板里的内容是否为浮点数!
valFloatClip(ev);
}
}
}
}
else
{
valNum(ev);
}
}
else
{
ev.returnValue = "";
}
}
//验证剪贴板里的内容是否为浮点数字!
function valFloatClip(ev)
{
//查看剪贴板的内容!
try
{
//如果不是数字将内容清空!
var content = clipboardData.getData("Text");
if(content != null)
{
var test = parseFloat(content);
var str = "" + test;
if(isNaN(test) == true)
{
//如果不是数字将内容清空!
clipboardData.setData("Text","");
}
else
{
if(str != content)
clipboardData.setData("Text", str);
}
}
}
catch(e)
{
//清空出现错误的提示!
alert("粘贴出现错误!");
}
}
//正则float
function validfloat(thisValue)
{
var rgExp=/((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))/;
var str=thisValue;
if(rgExp.exec(str))
{
return true;
}
else
{
return false;
}
}
</script>
</head>
<body>
<form runat="server">
正整数<input id="Text1" type="text" onkeydown=" valNum(event);" style="ime-mode: Disabled;" />
浮点数<input id="Text2" type="text" onkeydown=" valFloat(event,this.value);" style="ime-mode: Disabled;" />
</form>
</body>
</html>
onkeydown
onkeypress
这二个事件中判断
如何不符合就不要嘛...
if (math.search(......) != -1)
return true;
else
return false;
}
{
e.Handled = !(char.IsDigit(e.KeyChar) || (e.KeyChar == 8) );
}
KeyAscii = 0
End If
End I
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(char.IsDigit(e.KeyChar) ¦ ¦ (e.KeyChar == 8) );
}
If Len(Text1.Text) = 0 Then Exit Sub
If Not IsNumeric(Text1.Text) Then
Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
'控制输入
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
Beep
End If
ValiText = KeyOut
End Function
不好意思 分数不够分的了 只好给先答的而且思路与前面不同的了
再次谢谢每一位回帖的YDJM~~
第一步利用样式表。
<asp:TextBox ID="TextBox1" runat="server" style="ime-mode:disabled" onkeydown="myKeyDown()"></asp:TextBox>第二步利用js脚本。
<script type="text/javascript">
function myKeyDown()
{
var k=window.event.keyCode; if ((k==46)||(k==8)||(k==189)||(k==109)||(k==190)||(k==110)|| (k>=48 && k<=57)||(k>=96 && k<=105)||(k>=37 && k<=40))
{ }
else if(k==13)
{
window.event.keyCode = 9;
}
else
{
window.event.returnValue = false;
}
}
</script>利用样式表(style="ime-mode:disabled" )可以防止切换成汉字输入法,也就是说只能使用英文输入法,这样就防止了输入汉字的情况。js脚本就是要限制键盘输入,只能填入数字、小数点、负号、退格键、删除键和方向键。ascii码说明:
8:退格键
46:delete
37-40: 方向键
48-57:小键盘区的数字
96-105:主键盘区的数字
110、190:小键盘区和主键盘区的小数点
189、109:小键盘区和主键盘区的负号13:回车
9: Tab 就是那个把焦点移到下一个文本框的东东。对了还有两个缺点:
1、没有验证多个小数点的情况。
2、没有验证多个负号,和符号必须在前的情况。 http://88223100.cnblogs.com
页面里:
<head runat="server">
<SCRIPT language="javascript">
var lastValue = "";//存储上一个的值
//禁止输入非数字字符
function numberTextChange(tbox)
{
if(!/^\d+$/.test(tbox.value) && tbox.value!="")//非数字,赋值为原来的值
{
tbox.value = lastValue
}
else//为数字则更新存储值
{
lastValue = tbox.value;
}
}
</script>
</head>后台代码里:protected void Page_Load(object sender, EventArgs e)
{
//增加脚本
tbox.Attributes.Add("onpropertychange", "numberTextChange(this)");
//输入法设置
tbox.Style["ime-mode"] = "disabled";
}
If Len(TextBox1.Text) <= 1 Then
If Not IsNumeric(TextBox1.Text) Then
TextBox1.Text = ""
End If
Else
If Not IsNumeric(TextBox1.Text) Then
TextBox1.Text = Replace(TextBox1.Text, e.KeyChar, "")
End If
End If
End Su
{
public void chek_text(string strText)//添加一个方法
{
int start=0;
int over=strText.lengh;
int lengh=strText.lengh-1;
string str=@"[0-9]";
Regex regex=new Regex(str);
Match match_strText= Regex.Match(strText,start,over,lengh);
bool bool_str_Text=match_strText.success;
while(bool_Text)
{
if(start==over)
{
lengh--;
start++;
match match_strText=match_strText.nextMatch();
}
else
{
Console.WriteLine("完全匹配!");
return; }
}
Console.WriteLine("不完全匹配!");
}
}
//在需要得地方加入方法调用
chek_text(Textbox1.text);
<html>
<head>
<title>文本框限制</title>
</head><script type="text/javascript">
function regInput(reg)
{
var srcElem = event.srcElement;
var oSel = document.selection.createRange();
var srcRange = srcElem.createTextRange();
oSel.setEndPoint("StartToStart", srcRange);
var num = oSel.text + String.fromCharCode(event.keyCode) + srcRange.text.substr(oSel.text.length);
event.returnValue = reg.test(num);
}function fncKeyStop(evt)
{
if(!window.event)
{
var keycode = evt.keyCode;
var key = String.fromCharCode(keycode).toLowerCase();
if(evt.ctrlKey && key == "v")
{
evt.preventDefault();
evt.stopPropagation();
}
}
}</script><body>
只允许输入数字,且不能粘贴<br />
<input type="text" onkeypress="regInput(/^[0-9]*$/)" onkeydown="fncKeyStop(event)" onpaste="return false" oncontextmenu = "return false;"/>
</body>
</html>