/////////////////////////////// HTML
<input type="text" name="decimal" id="decimal" onkeydown="return Validate_NumerDecimal(this,event,8)">
//////////////////////////// JQUERY
<script>
function Validate_NumerDecimal(th, event, mxlen, decNum) {
decNum = (decNum == undefined || decNum == "" || decNum == null) ? 0 : decNum;
var keyArr = [0, 8, 36, 37, 38, 39, 46, 190];
var codes = event.keyCode || event.which;
if ($(th).val()=="" && codes == 190) {
return false
}
else if ((keyArr.indexOf(codes) == -1) && (codes < 48 || codes > 57)) {
return false;
}
else if ((keyArr.indexOf(codes) > 0)) {
if ($(th).val().indexOf('.') > 0 && codes == 190) {
return false
}
setTimeout(function () {
if ($(th).val().indexOf('.') == -1 && $(th).val().length > mxlen) {
let str = $(th).val();
$(th).val(str.substr(0, mxlen));
}
}, 10);
return true;
}
else {
var dotPos = $(th).val().indexOf('.');
if (dotPos < 0) {
if ($(th).val().length < mxlen || codes == 46) {
return true;
}
else {
return false;
}
}
else {
if ($(th).val().length >= dotPos && $(th).val().length < dotPos + (decNum + 1) && codes != 46) {
var number = th.value.split('.');
var caratPos = getSelectionStart(th);
if (caratPos <= dotPos) {
if (number[0].length < mxlen) {
return true;
}
else {
return false;
}
}
else if (caratPos > dotPos && dotPos > -1 && (number[1].length > (decNum))) {
return false;
}
else {
return true;
}
}
else {
return false;
}
}
}
function getSelectionStart(th) {
if (th.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveEnd('character', th.value.length)
if (r.text == '') return th.value.length
return th.value.lastIndexOf(r.text)
} else return th.selectionStart
}
}
}
else if ((keyArr.indexOf(codes) == -1) && (codes < 48 || codes > 57)) {
else if ((keyArr.indexOf(codes) > 0)) {
}
setTimeout(function () {
}
}, 10);
return true;
else {
else {
}
else {
else {
}
else if (caratPos > dotPos && dotPos > -1 && (number[1].length > (decNum))) {
else {
}
else {
}
}
function getSelectionStart(th) {
}
</script>
No comments:
Post a Comment