文本框应仅接受数字和点 [英] Text box should only accept only numbers and dot
本文介绍了文本框应仅接受数字和点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只需要在文本框中输入数字和一个点(。)。我如何使用代码验证它。不是通过asp控件。
I only need to type numbers and one dot(.) in my text box. How can i validate it using code. Not by asp controls.
推荐答案
您可以使用正则表达式 [ ^ ]通过 Javascript [ ^ ]。您可以使用此网站 [ ^ ]来测试你的Regex模式。
You can use Regular Expressions[^] to validate a textbox through Javascript[^]. You can use this site[^] to test your Regex patterns.
对于服务器端你可以用这种方式...... 。
For the server side you can use this way....
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
你可以使用这个javascript函数进行客户端验证。
You can use this javascript function for the client side validation.
<html>
<head>
<script language="Javascript">
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
//-->
</script>
</head>
<body>
<input id="txtChar" önkeypress="return isNumberKey(event)">
type="text" name="txtChar">
</input></body>
</html>
参考这个
仅键入文本框中的数字和点 [ ^ ]
这篇关于文本框应仅接受数字和点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文