jQuery form with price format I have this jQuery form with 4 text inputs.
The first one only accepts digits, the second one formats value into 'de-DE' price format accepting 2 decimals, the second does the same but accepts 5 decimals after decimal separator, fourth one automatically adds periods for date input.



    $(window).ready(function () {
             function validateNumber(event) {
     var $txtBox = $(this);
     var key = window.event ? event.keyCode : event.which;
                      //activates the Backspace button and right/left arrows
     if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 39) {
      return true;
     } // prevent if not number/dot
      else if ( key &lt; 44 || key &gt; 57 &amp;&amp; event.keyCode != 44) {
      event.preventDefault();
     }else {
     var len = $txtBox.val().length;
     var index = $txtBox.val().indexOf(',');
 
     if (index &gt; 0 &amp;&amp; event.keyCode == 44) {
     return false;
     }
     if (index &gt; 0) {
     var charAfterdot = (len + 1) - index;
     if (charAfterdot &gt; 6) {
      return false;
     }
    }
   }
      return $txtBox;
        };
            $('[id^=int]').keypress(validateNumber);
   $('[id^=float1]').keypress(validateNumber);
   $('[id^=float1]').on("blur", numberFormat);
   
     function numberFormat(event) {
        var number = parseFloat(this.value);
     console.log(this.value);
     console.log("before: " + number);
     console.log("after: " + number.toLocaleString('de-DE',{maximumFractionDigits: 5, style:'currency', currency:'EUR'}));
      this.value = isNaN(number) ? '0': number.toLocaleString('de-DE',{maximumFractionDigits:5, style:'currency', currency:'EUR'});
      var digits = maximumFractionDigits;
    };
   $('[id^=float2]').keypress(validateNumber);
   $('[id^=float2]').on("blur", numberFormat);
     var dt = dateFormatter('#date');
       $('[id^=date]').keypress(validateNumber);
        });
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt; 
 &lt;meta charset="utf-8"/&gt;
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;    
  &lt;script src="dateFormatter.js"&gt;&lt;/script&gt;
  &lt;script&gt;
        &lt;/script&gt;

 
&lt;/head&gt;
&lt;body style="position:relative;"&gt;
  &lt;form style="display:block;margin:0 auto;border:1px solid #aaa;border-radius:10px;width:215px;padding:10px;margin-top:100px;background-color:#F0F0F0;box-shadow:2px 2px 2px 2px rgba(0,0,0,0.2);"&gt;
   &lt;table&gt;
    &lt;tr&gt;
     &lt;td&gt;Number:&lt;/td&gt;
     &lt;td&gt;&lt;input id="int" type="input" value="0" size="5" maxlength="13" style=width:100px;margin-left:5px;,/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;FloatComma1:&lt;/td&gt;
     &lt;td&gt;&lt;input id="float1" decimalPlaces="2" type="text" maxlength="13" style=width:100px;margin-left:5px;/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;FloatComma2:&lt;/td&gt;
     &lt;td&gt;&lt;input id="float2" decimalPlaces="5" type="text" maxlength="13" style=width:100px;margin-left:5px;/&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
     &lt;td&gt;Date:&lt;/td&gt;
     &lt;td&gt;&lt;input id="date" type="text" value="" maxlength="10"  style=width:100px;margin-left:5px; /&gt;&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/table&gt;
  &lt;/form&gt;
 &lt;/body&gt;




As you can see, the FloatComma1 variable is allowing more than 2 decimals because my numberFormat function has this.value = number.toLocaleString('de-DE',{maximumFractionDigits:5});, so it allows 5.

Does anyone know a way how I could just replace the 5 with, let´s say a variable decimals, which I can give separate values to?
For example, I could call the variable in FloatComma1 decimals = 3 , and in FloatComma2 decimals = 5?

I do not know how to do it. Can someone help?
I'm solely going to post on your coding structure rather than the method of procedure for the task your code does.



In validateNumber(), your indentation starts out good and then goes downhill from there.


}else {
var len = $txtBox.val().length;
var index = $txtBox.val().indexOf(',');
if (index &gt; 0 &amp;&amp; event.keyCode == 44) {
return false;
}
if (index &gt; 0) {
var charAfterdot = (len + 1) - index;
if (charAfterdot &gt; 6) {
        return false;
}



with whitespace added would transform into:

} else {
    var len = $txtBox.val().length;
    var index = $txtBox.val().indexOf(',');
    if (index &gt; 0 &amp;&amp; event.keyCode == 44) {
        return false;
    }
    if (index &gt; 0) {
        var charAfterdot = (len + 1) - index;
        if (charAfterdot &gt; 6) {
            return false;
        }




Your naming conventions could be improved also:
For example, charAfterdot into charAfterDot.



Instead of blindly comparing random event.keyCodes with numbers, declare the numbers before hand as constants.  

const BACKSPACE = 8;
const TAB = 9;
const LEFT_ARROW = 37;
const RIGHT_ARROW = 39;


However, your version of JavaScript may not support constants, so use var in that case, and then just:

if (event.keyCode == BACKSPACE || event.keyCode == TAB || event.keyCode == LEFT_ARROW || event.keyCode == RIGHT_ARROW)


Declaring constants is a much clearer way of programming, than comparing to random numbers.