2014-03-19 4 views
0

"."다음에 최대 2 자리 숫자가있는 입력 값으로 사용자가 쓰기를 원합니다. 예 : 24.14 또는 5.7 또는 0.5 또는 8 및 4.321 또는 5.133121이 아닙니다. 어떻게해야합니까?입력 값에 4 자리 숫자 만 쓰도록 사용자를 제한하려면 어떻게합니까?

+0

가능한 복제본 [@ Html.Textbox에서 십진수로 입력하십시오.] (http://stackoverflow.com/questions/16646219/force-input-to-be-decimal- in-html-textbox) –

+0

운영자가 질문을 수정하기 전에 수정하는 것이 좋습니다. – Fury

답변

3

나는 여기 텍스트 입력 이를위한 해결책을 찾았습니다. 당신은 Jquery에서이 API를 사용할 수 있습니다 : click here

+0

흥미로운 플러그인 – Fury

0

당신은 어떤 신체에 어떤 일을 강요 할 수 있지만 사용자 유형 후 진수를 포맷 할 수

<input type="text" id="input_id"> 

//format decimal value while typing 
(function($) { 
    $.fn.currencyFormat = function() { 
     this.each(function(i) { 
      $(this).change(function(e){ 
       if(isNaN(parseFloat(this.value))) return; 
       this.value = parseFloat(this.value).toFixed(2); 
      }); 
     }); 
     return this; //for chaining 
    } 
})(jQuery); 


$(function() { 
    $("#input_id").currencyFormat();  
}); 

Resource

0

당신은 할 수 문자열로 숫자를 변환 .toFixed() 방법의 사용 지정된 소수 자릿수를 유지합니다.

샘플 :

var myNum = document.getElementById("textboxID").value; 
myNum = parseFloat(myNum).toFixed(2); //I fixed my precision to 2 digits after "." 
0

이 당신을 위해 작동 할 수 있습니다

<input type="text" id="tb1" > 

그리고 자바 스크립트/jQuery 코드 :

$('#tb1').on('keydown',function(e){ 
var textValue=document.getElementById('tb1').value; 
if(textValue.indexOf('.')>=0 && e.which!=8 && e.which!=37 && e.which!=39 && e.which!=46) 
    { 
    textValue=textValue.split('.')[1]; 
    if(textValue.length>=2) 
     return false; 
    } 
}); 
관련 문제