Entrepreneur
Entrepreneur

Reputation: 45

How to detect key entered at nth digit

I'm trying here to convert Unicode number to normal number. So far I have managed to convert it for 1st digit. Eg: १ => 1

But when number is 2 digit or more it fail to detect it.

It would be very helpful if anyone could point out how to solve this...

var items = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'];
$("#xyz").keyup(function(){
    var y = $("#xyz").val();

    if(jQuery.inArray(y, items) !== -1){
        $("#xyz").val(jQuery.inArray(y, items));
    }else{
        console.log('other');
    }

});

Update

Have managed to convert for nth digit. Only issue is to replace entered string with result. Getting correct output in console but can't replace it in input box

Jsfiddle Link

var items = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'];
$("#xyz").keyup(function(){
    var y = $("#xyz").val();
    var res = y.split("");

    var arr = [];
    res.forEach(function(xx) {
        if(jQuery.inArray(xx, items) !== -1){
            arr.push(jQuery.inArray(xx, items));
        }else{
            console.log('other');
        }
    });

    console.log(arr.join(""));
});

Upvotes: 0

Views: 33

Answers (1)

Scaramouche
Scaramouche

Reputation: 3267

I was not able to find a way to type those characters directly into the input, so testing was not as thorough as I would want it but, give this a try:

var items = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'];
$("#xyz").keyup(function(){
    var y = $("#xyz").val();
    var res = y.split("");
    
    var arr = [];
    res.forEach(function(xx) {
        if(jQuery.inArray(xx, items) !== -1){
            arr.push(jQuery.inArray(xx, items));
        } else if(xx.match(/\d/)){
        arr.push(xx);
        }else{
            console.log('other');
        }
    });

    console.log(arr.join(""));
    $(this).val(arr.join(""));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="xyz"/>

Upvotes: 1

Related Questions