How can the script automatically change the entered comma to a point immediately when entering a comma?

    1 answer 1

    $('input#hax').on('input', function() { $(this).val($(this).val().replace(/\,/g, '.')); $(this).val($(this).val().replace( /(?=(\d+\.\d{2})).+|(\.(?=\.))|([^\.\d])|(^\D)/gi, '$1')); }) 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='text' id='hax' /> 

    • SLy_huh thanks! Can you add a condition here that after a point you can no longer enter a point and you can enter a maximum of two digits? - Eugene
    • in the case of testing by pressing the key is not universal. for example, it will not work when adding a value via ctrl + V or the right mouse button - lexxl
    • lexxl how to be then? .on ("input" also did not help for ctrl + V, insertion with the mouse is disabled, therefore it is not so important. - Eugene
    • @ Eugene changed the code, now it works on insertion - SLy_huh
    • Thank! Works! - Eugene