0

I have HTML input name "sectoral_txt" that has regex on it.

<input type="text" name="sectoral_txt" class="numberwithdecimals">

jQuery for class name "numberwithdecimals"

const format = (str) => (""+str)                                            
                                                    .replace(/[^\d.-]|(?!^)-/g, "")
                                                    .replace(/^([^.]*\.)(.*$)/, (_, g1, g2) => g1 + g2.replace(/\./g, ''))
                                                    .replace(/\.(\d{2})\d+/, '.$1')
                                                    .replace(/\B(?=(\d{3})+(?!\d))/g, ",")              
        const parse = (str) => str
                                        .replace(/,/g,'')
                                        .replace(/^([^.]*\.)(.*$)/, (_, g1, g2) => g1 + g2.replace(/\./g, ''));

        $('input.numberwithdecimals').keyup(function(event) 
        {
            if (event.which >= 37 && event.which <= 40) return;
            $(this).val((i,v) => formatCurrency(v));
        });

When I enter number in input field above, the value will determine the rating. Index 0 will be <= 4.70 , Index 1 until 5 is within range, and Index 6 will >= 21.00. Class name for Rating is (check_rtg) and Sectoral Ratio is (check_rto)

Rating (check_rtg)          Sectoral Ratio (check_rto)

AAA                         4.70
AAA                         6.30
A                           9.10
BBB                         12.50
BB                          17.70
B                           19.60
CCC                         21.00

I want to check the value given and display the rating using jQuery. For example, if my input 10.00, Rating will be BBB because its in between 12.50 and 9.10

Here's my current code in jQuery :

$("#sectoral_txt").keyup(function() {

            var selected    = parse( $(this).val() );

            if($('#sectoral_txt').val() == '')
            {
                alert('Empty Field');
            }
            else
            {
                var rating = 0;
                $('.check_rto').each(function(index) 
                {
                    var sectoral_ratio  = parse( $(this).val() );

                    // Index 0
                    if( selected <= sectoral_ratio )
                    {               
                        rating = $(".check_rtg").eq(index).val();
                    }

                    // Index 1 until 5
                    else if( selected >= sectoral_ratio && selected < sectoral_ratio )
                    {
                        rating = $(".check_rtg").eq(index).val();
                    }

                    // Index 6
                    else if( selected >= sectoral_ratio )
                    {
                        rating = $(".check_rtg").eq(index).val();
                    }
                    else
                    {
                        console.log('error');
                    }
                }); 
            }
        });

But my code below always execute "Index 6". Can someone help me to solve this problem ? Thanks in advance.

Rzj Hayabusa
  • 657
  • 2
  • 11
  • 29

0 Answers0