title: Prevent multiple decimals points in calculation field
body: <p>I have a JavaScript calculator which uses the following code to handle decimal points in the calculation input field (also below):</p>
  
  <pre><code>$('#button-dot').click(function() {
  var lastChar = $('#disp').val().slice(-1);
  var firstChar = $('#disp').val().slice(0);
  if ($('#disp').hasClass("result")) {
        $('#disp').removeClass("result").val("");
        addChar(this.form.display,'0' + '.');
      }
  else if (lastChar == '.'){
      // DO NOTHING
      }
  else if (lastChar == '1' || lastChar == '2' || lastChar == '3' || lastChar == '4' || lastChar == '5' || lastChar == '6' || lastChar == '7' || lastChar == '8' || lastChar == '9' || lastChar == '0' &amp;&amp; firstChar != '0'){
      addChar(this.form.display,'.');
      }
  else if (firstChar == '0'){
      addChar(this.form.display,'0' + '.');
      }
  else {
    addChar(this.form.display,'0' + '.');
    }
    $('#disp').removeClass("result");
  });
  </code></pre>
  
  <p>where the <code>addChar</code> function is given by:</p>
  
  <pre><code>function addChar(input, character) {
    if (input.value == null || input.value == "0" ) {
      input.value = character
      }
    else {
      input.value += character
      }
  };
  </code></pre>
  
  <p>and the input field:</p>
  
  <pre><code>&lt;INPUT NAME="display" ID="disp" VALUE="0" SIZE="28" MAXLENGTH="25"/&gt;
  </code></pre>
  
  <p>I want to enhance my code in such a way as to restrict users from entering multiple decimals in the same number (as seen below), while still allowing multiple decimals in the calculation string (also below):</p>
  
  <p>Avoid this -</p>
  
  <p><a href="https://i.stack.imgur.com/0G3i4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0G3i4.png" alt="enter image description here"></a> </p>
  
  <p>But allow this -</p>
  
  <p><a href="https://i.stack.imgur.com/5WcSR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5WcSR.png" alt="enter image description here"></a> </p>
  
  <p>I have looked into <code>regex</code> as I understand the solution to my problem may involve it somehow, but I am not sure how to implement it (my JavaScript skills are still a work in progress!). </p>
  
  <p>I also thought of splitting the string with any of the operands (<code>-,+,/,*,%</code>) and checking each element of the resulting array for a decimal point, but I am thinking that might be a tad messy.</p>
  
  <p>Any ideas?</p>