2023-04-28 12:22:26 +08:00

177 lines
5.0 KiB
JavaScript

//监听键盘输入是否为数字
function MouseDownCheckNumber(evt, object) {
var length = object.value.length;
var textvalue = object.value;
var c = evt.keyCode;
var nlenselect;
var nstart;
var nend;
var ch;
var part;
nlenselect = object.selectionEnd - object.selectionStart;
nstart = object.selectionStart;
nend = object.selectionEnd;
if (nstart === undefined) {
try {
var rng;
if (object.tagName.toLowerCase() == "textarea") { //TEXTAREA
rng = evt.srcElement.createTextRange();
rng.moveToPoint(evt.x, evt.y);
} else { //Text
rng = document.selection.createRange();
}
nlenselect = rng.text.length;
rng.moveStart("character", -evt.srcElement.value.length);
nend = rng.text.length;
nstart = Math.max(0, nend - nlenselect);
} catch (e) {
throw new Error(10, "failed to get cursor position.")
}
}
if (nlenselect > 0) {
part = object.value.substring(nstart, object.selectionEnd);
}
else {
part = "";
}
/*-- Control the "-" key----*/
if (((c == 173 || c == 189 || c == 109) && !evt.shiftKey) || c == 229) {
return false;
}
/*--- Control the dot key--*/
else if ((c == 190 || c == 110) && !evt.shiftKey) {
if (textvalue.indexOf('.') == -1) {
if (length == 0) {
}
else if (nstart == 0) {
if (part.length == 0 && textvalue.charCodeAt(0) != 45) {
}
else if (part.length != 0) {
}
else {
return false;
}
}
}
else {
if (part.indexOf('.') != -1) {
}
else {
return false;
}
}
}
else if ((c >= 35 && c <= 40) || c == 46 || c == 9) {
}
/* Control the Number Key*/
else if (((evt.keyCode >= 48 && evt.keyCode <= 59) && !evt.shiftKey) || (c >= 96 && c <= 105)) {
if (length == 0) {
}
else if (length != 0) {
/* check the number length behind the dot*/
var op = length - textvalue.indexOf('.');
if (textvalue.indexOf('.') != -1) {
if (part.length != 0) {
}
else if (nstart > textvalue.indexOf('.')) {
if (object.lengthbehinddot != null) {
if ((length - textvalue.indexOf('.')) > object.lengthbehinddot) {
return false;
}
}
}
}
/* check ... */
ch = textvalue.charCodeAt(0);
if (nstart == 0 && part.length != 0) {
}
else if (nstart == 0 && ch == 45) { // -
return false;
}
}
}
/* Open the Ctrl + C Copy Key*/
else if ((evt.keyCode == 67 || evt.keyCode == 99) && evt.ctrlKey) {
}
/* Open the Ctrl + V Paste Key*/
else if ((evt.keyCode == 86 || evt.keyCode == 118) && evt.ctrlKey) {
}
/* Open the Ctrl + Z Back Key*/
else if ((evt.keyCode == 90 || evt.keyCode == 122) && evt.ctrlKey) {
}
/* Open the Esc Key*/
else if (evt.keyCode == 27) {
}
else if ((evt.keyCode < 48 || evt.keyCode > 59) && evt.keyCode != 8 || evt.shiftKey) {
return false;
}
return true;
}
//对输入框的输入类型控制
function InputControl(object) {
object.mouseup(function () { return false; });
object.keydown(function (evt) {
var c = evt.which;
if (c == 13) {
tmp = $(this).attr("id");
setTimeout(EnterFocus, 200);
return false;
}
else {
if (c != 9) {
var b = MouseDownCheckNumber(evt, $(this)[0]);
if (!b)
return false;
}
}
});
}
function inputValueFormat(obj) {
var curval =$.trim($.trim($(obj).val()));
var floatvalue = parseFloat(curval);
if (floatvalue <= 0 || isNaN(floatvalue) || curval == "")
return false;
else {
$(obj).val(floatvalue);
return true;
}
}
function checkDate(obj) {
var splarray = obj.split('/');
if (splarray.length != 3) {
return false;
}
else {
/*
* 不易判断多种语言环境下的不同顺序格式,例如 2021/9/13 与 9/13/2021
*
var mm=parseInt(splarray[0]);
var dd=parseInt(splarray[1]);
var yyyy=parseInt(splarray[2]);
if (mm > 0 && mm <= 12 && dd > 0 && dd <= 31 && yyyy >= 1900) {
return true;
} else {
return false;
}
*/
var date = new Date(obj);
if (isNaN(date.getTime())) {
return;
}
if (date.getFullYear() < 1900) {
return false;
}
return true;
}
}