9 个解决方案
#1
你是要判断还是要限制
#2
前段时间写着玩的小东西,有你想要的东西
<html>
<head>
<title>加减乘除</title>
<script type="text/javascript">
window.onload = function () {
var txtNumber1Emement = document.getElementById("txtNumber1");
var txtNumber2Emement = document.getElementById("txtNumber2");
var operatorTypeEmlement = document.getElementById("operatorType");
txtNumber1Emement.onkeydown = txtNumber2Emement.onkeydown = function (e) {
e = e || window.event;
var keyCode = e.keyCode || e.charCode;
//清除键
if (keyCode == 8) {
return;
}
//回车键
if (keyCode == 13) {
doWork();
return;
}
//如果不是数字阻止输入
if (keyCode < 48 || keyCode > 57) {
if (typeof e.preventDefault == "function") {
e.preventDefault();
e.stopPropagation();
}
else {
e.returnValue = false;
e.cancelBubble = true;
}
}
}
operatorTypeEmlement.onchange = function () {
doWork();
}
}
function doWork() {
var txtNumber1Value = document.getElementById("txtNumber1").value || 0;
var txtNumber2Value = document.getElementById("txtNumber2").value || 0;
var value1 = parseInt(txtNumber1Value, 10);
var value2 = parseInt(txtNumber2Value, 10);
var operator = document.getElementById("operatorType").value;
document.getElementById("result").value = eval(value1 + operator + value2);
}
</script>
</head>
<body>
<fieldset style="width: 600px; height: 500px; margin: 0px;">
<input type="text" id="txtNumber1" style="width: 270px; border-right-width: 0px;
text-align: right" /><select id="operatorType" style="width: 163px; border-left-width: 0px">
<optgroup>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</optgroup>
</select><br />
<input type="text" id="txtNumber2" style="width: 270px" /><br />
=
<input type="text" id="result" disabled="disabled" style="width: 100%" />
</fieldset>
</body>
</html>
#3
在keypress里面写
#4
//输入数字
function isNumber(txt,name)
{
var str=$(txt).value;
var pattern =/^[0-9]*$/;
if(str!="" && (!pattern.exec(str)))
{
alert(name+"请输入数字!");
return false;
}
else
{
return true;
}
}
#5
在文本框的keydown事件里面写,如果在keypress里面写,如果有用用户控件并且在用户控件中有用系统自带验证非空会出问题。
#6
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
string key = this.textBox1.Text;
if (e.KeyCode == Keys.Enter)
{
Regex reg = new Regex(@"[\d]+");
if (reg.IsMatch(key))
{
MessageBox.Show("number");
}
else
{
MessageBox.Show("others");
}
}
}
你应该是这个意思吧,当按下回车键时判断是否是数字,这个代码是当按下回车键时如果是数字则提示‘number’
否则提示'others'
这个代码如果一直按回车键,则一直有提示。关掉提示的方法时用鼠标点击就OK了!
{
string key = this.textBox1.Text;
if (e.KeyCode == Keys.Enter)
{
Regex reg = new Regex(@"[\d]+");
if (reg.IsMatch(key))
{
MessageBox.Show("number");
}
else
{
MessageBox.Show("others");
}
}
}
你应该是这个意思吧,当按下回车键时判断是否是数字,这个代码是当按下回车键时如果是数字则提示‘number’
否则提示'others'
这个代码如果一直按回车键,则一直有提示。关掉提示的方法时用鼠标点击就OK了!
#9
#1
你是要判断还是要限制
#2
前段时间写着玩的小东西,有你想要的东西
<html>
<head>
<title>加减乘除</title>
<script type="text/javascript">
window.onload = function () {
var txtNumber1Emement = document.getElementById("txtNumber1");
var txtNumber2Emement = document.getElementById("txtNumber2");
var operatorTypeEmlement = document.getElementById("operatorType");
txtNumber1Emement.onkeydown = txtNumber2Emement.onkeydown = function (e) {
e = e || window.event;
var keyCode = e.keyCode || e.charCode;
//清除键
if (keyCode == 8) {
return;
}
//回车键
if (keyCode == 13) {
doWork();
return;
}
//如果不是数字阻止输入
if (keyCode < 48 || keyCode > 57) {
if (typeof e.preventDefault == "function") {
e.preventDefault();
e.stopPropagation();
}
else {
e.returnValue = false;
e.cancelBubble = true;
}
}
}
operatorTypeEmlement.onchange = function () {
doWork();
}
}
function doWork() {
var txtNumber1Value = document.getElementById("txtNumber1").value || 0;
var txtNumber2Value = document.getElementById("txtNumber2").value || 0;
var value1 = parseInt(txtNumber1Value, 10);
var value2 = parseInt(txtNumber2Value, 10);
var operator = document.getElementById("operatorType").value;
document.getElementById("result").value = eval(value1 + operator + value2);
}
</script>
</head>
<body>
<fieldset style="width: 600px; height: 500px; margin: 0px;">
<input type="text" id="txtNumber1" style="width: 270px; border-right-width: 0px;
text-align: right" /><select id="operatorType" style="width: 163px; border-left-width: 0px">
<optgroup>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</optgroup>
</select><br />
<input type="text" id="txtNumber2" style="width: 270px" /><br />
=
<input type="text" id="result" disabled="disabled" style="width: 100%" />
</fieldset>
</body>
</html>
#3
在keypress里面写
#4
//输入数字
function isNumber(txt,name)
{
var str=$(txt).value;
var pattern =/^[0-9]*$/;
if(str!="" && (!pattern.exec(str)))
{
alert(name+"请输入数字!");
return false;
}
else
{
return true;
}
}
#5
在文本框的keydown事件里面写,如果在keypress里面写,如果有用用户控件并且在用户控件中有用系统自带验证非空会出问题。
#6
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
string key = this.textBox1.Text;
if (e.KeyCode == Keys.Enter)
{
Regex reg = new Regex(@"[\d]+");
if (reg.IsMatch(key))
{
MessageBox.Show("number");
}
else
{
MessageBox.Show("others");
}
}
}
你应该是这个意思吧,当按下回车键时判断是否是数字,这个代码是当按下回车键时如果是数字则提示‘number’
否则提示'others'
这个代码如果一直按回车键,则一直有提示。关掉提示的方法时用鼠标点击就OK了!
{
string key = this.textBox1.Text;
if (e.KeyCode == Keys.Enter)
{
Regex reg = new Regex(@"[\d]+");
if (reg.IsMatch(key))
{
MessageBox.Show("number");
}
else
{
MessageBox.Show("others");
}
}
}
你应该是这个意思吧,当按下回车键时判断是否是数字,这个代码是当按下回车键时如果是数字则提示‘number’
否则提示'others'
这个代码如果一直按回车键,则一直有提示。关掉提示的方法时用鼠标点击就OK了!