I have the below code which validates for 18,2 that means it allows 18 digits and after 18 digits it will add a (.) and after (.) it will accept only two numbers which is fine but if i am again adding numbers before (.) it is taking which should not happen
how can i resolve this
function fnValidate() { //new
$('#txt').keypress(function (event) {
debugger;
var $this = $(this);
if ((event.which != 46 || $this.val().indexOf('.') != -1) && ((event.which < 48 || event.which > 57) && (event.which != 0 && event.which != 8))) {
event.preventDefault();
}
var text = $(this).val();
if (text.length === 18) {
$(this).val(text + ".")
}
if ((event.which == 46) && (text.indexOf('.') == -1)) {
setTimeout(function () {
if ($this.val().substring($this.val().indexOf('.')).length > 3) {
$this.val($this.val().substring(0, $this.val().indexOf('.') + 3));
}
}, 1);
}
if ((text.indexOf('.') != -1) && (text.substring(text.indexOf('.')).length > 2) && (event.which != 0 && event.which != 8) && ($(this)[0].selectionStart >= text.length - 2)) {
event.preventDefault();
}
});
}