0

I have an HTML number field. This field value assigned throw following error

The specified value "101,5" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?

I need a value to convert to french format (de-DE). My try is

var dispersePercentageInput = $('.disperse-percentage');
var percentage = 101.5
dispersePercentageInput.val(Number(percentage).toLocaleString('de-DE'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" name="etude_disbursement_rate" value="100.00" class="disperse-percentage required" step="0.01" data-prefix="-" required="" id="id_etude_disbursement_rate" title="S'il vous plaît remplissez cet champ">

I expect value is 101,5 but it shows The specified value "101,5" is not a valid number.

My input field is

<input type="number" name="etude_disbursement_rate" value="100.00" class="disperse-percentage required" step="0.01" data-prefix="-" required="" id="id_etude_disbursement_rate" title="S'il vous plaît remplissez cet champ">

User input in en-US format

shafik
  • 6,098
  • 5
  • 32
  • 50
  • 2
    Working fine for me, can you reproduce in snippet? – Just code Jan 01 '19 at 04:49
  • I am using `jQuery v1.11.2` version. Is this produce error? – shafik Jan 01 '19 at 04:58
  • No, it has nothing to do with jquery version. – Just code Jan 01 '19 at 04:59
  • What does your actual input HTML look like? Is it the same as what was added by Mamun? – H77 Jan 01 '19 at 05:01
  • I can see the desired value in the console. But it not shows in input field. – shafik Jan 01 '19 at 05:03
  • I don't think `number` inputs currently accept commas. Try entering a comma into the input in the snippet above. Your only option might be to use a `text` type input. – H77 Jan 01 '19 at 05:12
  • I convert input field to text. ANd it's working fine – shafik Jan 01 '19 at 05:16
  • Possible duplicate of [HTML5 input box with type="number" does not accept comma in Chrome browser](https://stackoverflow.com/questions/35315157/html5-input-box-with-type-number-does-not-accept-comma-in-chrome-browser) – H77 Jan 01 '19 at 05:29

3 Answers3

3

Please use "autoNumeric.min.js"

<script src="~/Scripts/autoNumeric/autoNumeric.min.js" type="text/javascript"></script>

add input tag in HTML file :

 <input type="text" class="form-control" id="numberint" data-a-dec="," data-a-sep="." data-a-sign="">

Script code :

<script type="text/text/javascript">
 $(function() {
    $('#numberint').autoNumeric('init');   
 });
</script>
Chetan
  • 198
  • 10
1

yeah, of course. input:type=number is not a good choice, it can just accept common format, but your format is very infrequent.

and number type has some other defects, such as

  1. not behave all the same in different browsers
  2. allows multiple .
  3. allows letter e

I recommend use just text type instead, and check format by RegExp or other way

Johnson Lee
  • 179
  • 3
0
var formatter = new Intl.NumberFormat('de-DE', {
    minimumFractionDigits: 2
});
console.log("Output: "+formatter.format(sum));

This working fine for me.

shafik
  • 6,098
  • 5
  • 32
  • 50