fatherazrael
fatherazrael

Reputation: 5977

How to completely validate a decimal value in JQuery?

How to validate following in Decimal value using Jquery?

I have created basic fiddle where i am able to validate number.

I am not allowing user to enter anything other then number, Dot(.) or Comma(,) (interchangeable), Backspace

HTML

<input type="text" class="myTexbox" />

JQUERY

$(document).ready(function () {
    $('.myTexbox').keypress(function (event) {
        return isDecimalNumber(event, this)
    });
});

function isDecimalNumber(eve, element) {
    var charCode = (eve.which) ? eve.which : event.keyCode
    if (
    (charCode != 8) //For Backspace Active
    &&
    (charCode != 44 || $(element).val().indexOf(',') != -1)
    //For comma
    &&
    (charCode != 46 || $(element).val().indexOf('.') != -1) && (charCode < 48 || charCode > 57))

    return false;
    return true;
}

Complete Fiddle: https://jsfiddle.net/s2t8fgn3/

Could you please help me in achieving that User can enter only two places after . or , like 123.02 only and Comma or Dot once inserted should not be repeated.

Update: Also we need to Copy paste into it. so how to manage that whether data is proper decimal and how to allow CTRL+V but not V.

Upvotes: 2

Views: 231

Answers (2)

rhorvath
rhorvath

Reputation: 3725

I would add regexp check

$(element).val().toString().match("\\.[0-9]{2,}$")

see https://jsfiddle.net/2fpcg0ee/

update: different approach where you compare the value before and after

$(document).ready(function() {
    var oldVal = '';
    $('.myTexbox').keypress(function (event) {
        if (this.value.match("^[0-9]*[.,]?[0-9]{0,2}$"))
            oldVal = this.value;
    });
    $('.myTexbox').keyup(function (event) {
        if (!this.value.match("^[0-9]*[.,]?[0-9]{0,2}$")) {
            $('.myTexbox').val(oldVal);   
        }
    });
});  

see https://jsfiddle.net/2fpcg0ee/4/

Upvotes: 3

Kapsonfire
Kapsonfire

Reputation: 1033

I made some code, I hope it's that what you tried to achieve

function validateDecimal(num) {
    //goal: decimal number, only one [.,] allowed, only 2 digits after [.,]

    //first we check for invalid numbers
    var findInvalid = num.match(/[^0-9\,\.]/g);
    if(findInvalid) //found invalid chars
        return false;


    //now replace , to . (dot is more machinelike)
    var num = num.replace(/,/g,".");

    //now find number of occurences of .
    var count = (num.match(/\./g) || []).length;
    if(count > 1) 
        return false; //we have more than one . -> so its invalid

    //now we count the chars after the first .
    var indexOfDot = num.indexOf(".");
    if(indexOfDot == -1) 
        return true; //we have no dot - so its pure numeric (no decimal)
    var charsAfterDot = num.length - (indexOfDot+1);
    if(charsAfterDot > 2)
       return false; //we have more than 2 chars after dot - invalid

    return true; //every other case is valid
}

http://jsfiddle.net/7Ls7L3u9/ <<< to fiddle around

Upvotes: 0

Related Questions