我有一个文本框,我希望它允许输入从0.00到24.00的数字。
<asp:TextBox ID = "txtHours" runat = "Server"
onkeydown="limit(this,event);"
onkeypress="return validateFloatKeyPress(this,event);"
MaxLength="5" Width = "40" text = '<%#showData(Container.DataItem, "Hours")%>'/>
我尝试在onkeypress
和onkeydown
它仍然允许输入数字,如25,26,25.01 ...等
我希望它甚至不允许在输入2之后键入5,6,7 ...等。
function validateFloatKeyPress(el, evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var number = el.value.split('.');
var numberbfr = el.value.split('.')[0];
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
if (number.length > 1 && charCode == 46) {
return false;
}
if (numberbfr.length > 1) {
return false;
}
var caratPos = getSelectionStart(el);
var dotPos = el.value.indexOf(".");
if (caratPos > dotPos && dotPos > -1 && (number[1].length > 1)) {
return false;
}
return true;
}
function getSelectionStart(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveEnd('character', o.value.length)
if (r.text == '') return o.value.length
return o.value.lastIndexOf(r.text)
} else return o.selectionStart
}
function limit(el, evt) {
if (parseInt(el.value.charAt(0)) > 2 || (parseInt(el.value.charAt(0)) = 2 && parseInt(el.value.charAt(1)) > 4)) {
return false;
}
}
答案 0 :(得分:0)
试试这个。希望这个解决方案可以帮助你。复制并粘贴下面的代码。&#39; stack&#39;是文本框的ID。
function checkOnlyZeroTo24(evt, el) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (el.value == '' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
return true;
}
else if (el.value != '') {
if (el.value.charAt(0) < 3 && charCode == 46 && el.value.length == 1 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.length == 1) {
document.getElementById('stack').setAttribute('maxlength', 5);
if (el.value.charAt(0) == 2 && (charCode < 48 || charCode > 52))
return false;
else
return true;
}
else if (el.value <= 24 && el.value >= 10 && charCode == 46 && el.value.indexOf('.') == -1) {
document.getElementById('stack').setAttribute('maxlength', 5);
return true;
}
else if (el.value.substr(0, el.value.indexOf('.')) <= 24 && el.value.substr(0, el.value.indexOf('.')) >= 10 && el.value.charAt(2) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57))) {
if (el.value.substr(0, el.value.indexOf('.')) == 24 && !(charCode == 48))
return false;
else
return true;
}
else if (el.value.charAt(0) < 3 && !(charCode > 31 && (charCode < 48 || charCode > 57)) && el.value.charAt(1) == '.') {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
else if ((el.value.charAt(0) >= 3 && charCode == 46 && el.value.indexOf('.') == -1) || (el.value.charAt(1) == '.' && !(charCode > 31 && (charCode < 48 || charCode > 57)))) {
document.getElementById('stack').setAttribute('maxlength', 4);
return true;
}
}
return false;
}
天冬氨酸:文本框
<asp:TextBox ID = "stack" runat = "server" onkeypress="return checkOnlyZeroTo24(event,this);"> </asp:TextBox>