Force decimal point instead of comma in HTML5 number input (client-side)

后端 未结 15 625
情话喂你
情话喂你 2020-11-29 03:05

I have seen that some browsers localize the input type=\"number\" notation of numbers.

So now, in fields where my application displays longitude and la

相关标签:
15条回答
  • 2020-11-29 03:58

    I have written a custom piece of code to do this

    If you want to replace , with ., remove translate_decimals functions completely.

    var input = document.querySelector('input[role="custom-number"]');
    var bincr = document.querySelector('button[role="increment"]');
    var bdecr = document.querySelector('button[role="decrement"]');
    
    function translate_decimals(side = 0)
    {
    	input.value = (side == ',' ? input.value.replace('.',',') : input.value.replace(',','.'));
    }
    translate_decimals(',');
    
    bincr.addEventListener('click', ()=>{
    	if (input.hasAttribute('max'))
    	{
    		if (input.value.substr(0,input.getAttribute('max').length) == input.getAttribute('max').substr(0,input.getAttribute('max').length))
    		{
    			return;
    		}
    		else
    		{
    			translate_decimals('.');
    			let temp = input.value;
    			input.value = "";
    			input.value = (input.hasAttribute('step') ? (parseFloat(temp) + parseFloat(input.getAttribute('step'))) : temp++);
    			translate_decimals(',');
    		}
    	}
    });
    
    bdecr.addEventListener('click', ()=>{
    	if (input.hasAttribute('min'))
    	{
    		if (input.value.substr(0,input.getAttribute('min').length) == input.getAttribute('min').substr(0,input.getAttribute('min').length))
    		{
    			return;
    		}
    		else
    		{
    			translate_decimals('.');
    			input.value = (input.hasAttribute('step') ? (input.value - input.getAttribute('step')) : input.value--);
    			translate_decimals(',');
    		}
    	}
    });
    /* styling increment & decrement buttons */
    button[role="increment"],
    button[role="decrement"] {
    	width:32px;
    }
    <input type="text" role="custom-number" step="0.01" min="0" max="0" lang="en" value="1.99">
    <button role="increment">+</button>
    <button role="decrement">-</button>

    0 讨论(0)
  • 2020-11-29 04:00

    Sadly, the coverage of this input field in the modern browsers is very low:

    http://caniuse.com/#feat=input-number

    Therefore, I recommend to expect the fallback and rely on a heavy-programmatically-loaded input[type=text] to do the job, until the field is generally accepted.

    So far, only Chrome, Safari and Opera have a neat implementation, but all other browsers are buggy. Some of them, don't even seem to support decimals (like BB10)!

    0 讨论(0)
  • 2020-11-29 04:00

    one option is javascript parseFloat()... never do parse a "text chain" --> 12.3456 with point to a int... 123456 (int remove the point) parse a text chain to a FLOAT...

    to send this coords to a server do this sending a text chain. HTTP only sends TEXT

    in the client keep out of parsing the input coords with "int", work with text strings

    if you print the cords in the html with php or similar... float to text and print in html

    0 讨论(0)
提交回复
热议问题