Search code examples
javascriptnumberssuffix

HTML input with numbers only and suffix


Im using inputAffix.js lib for showing suffix in html input. I want that field can be filled only with numbers, but also to contain text suffix.

	$(function(){
		$("#amount").suffix(" EUR");
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script src="https://zdravem.com/js/InputAffix.js"></script>

<input id="amount" class="form-control" placeholder="30 EUR"></input>


Solution

  • Hi you can try below code

    $(function(){
       $("#amount").suffix(" EUR");
    });
    		
    function isNumberKey(evt) {
       var charCode = (evt.which) ? evt.which : event.keyCode
       if (charCode > 31 && (charCode < 48 || charCode > 57)){
           return false;
       }
       return true;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    
    <script src="https://zdravem.com/js/InputAffix.js"></script>
    
    <input id="amount" class="form-control" placeholder="30 EUR" onkeypress="return isNumberKey(event)"></input>

    Hope it will help you