应在输入类型编号中接受零,但不能在负值中接受零

时间:2017-12-05 09:01:02

标签: javascript jquery css angularjs html5

我正在为输入类型做一些验证。

<input 
    type="number" 
    min="0" 
    decimal-places="" 
    ng-model="candidate.ctc" 
    class="form-control ng-pristine ng-untouched ng-valid-min ng-invalid ng-invalid-required candidateCTCInputPosition" 
    id="ctc" 
    placeholder="CCTC" 
    autocomplete="off" 
    required="">

此处,此用户无法将0作为输入,因此,当用户键入0时,它会将其视为false值。

因此,用户不应该使用keyups键入负值,也应该接受0。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

这是一个有效的解决方案: http://jsfiddle.net/qoL16sup/

<强>指令:

<form name="myForm" novalidate autocomplete="off">
<label>
  Decimal places = 0
  <input 
    type="text"
    name="input1"
    decimal-places="0" 
    ng-model="model.input1" 
    autocomplete="off" 
    required>
</label>
<br/> <br/>
<lable>
Decimal places = 2:
  <input 
    type="text"
    name="input2"
    decimal-places="2" 
    ng-model="model.input2" 
    autocomplete="off" 
    required>
</lable>

<br/> <br/>
<lable>
Decimal places = 2, min = 100, max = 10000:
  <input 
    type="text"
    name="input3"
    decimal-places="2" 
    min="100"
    max="10000"
    ng-model="model.input3" 
    autocomplete="off" 
    required>
</lable>
</form>

<强> HTML:

for(;;){
   QFile *file = new QFile("some.txt");//this is where it fails with "device not open" when windows screen is locked
   if(file->exists()){
      file->open(QFile::ReadOnly);
      QString contents = file->readAll();
      file->close();
   }
   file->deleteLater();
   QThread::sleep(2);
}

答案 1 :(得分:0)

您可以使用ngPattern验证输入。但它不会阻止用户输入负数。使用以下函数以避免键入负值。

	$(document).ready(function(){
	 $("#ctc").keydown(function(e){
	 
        if (e.key == "." || e.key=="-") {
            e.preventDefault();
        }
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Preventing negative values</p>
<input type="number" min="0" max="10" decimal-places="" ng-model="candidate.ctc" class="form-control ng-pristine ng-untouched ng-valid-min ng-invalid ng-invalid-required candidateCTCInputPosition" id="ctc" placeholder="CCTC" autocomplete="off" style='width:300px' required=""/>

答案 2 :(得分:0)

你应该这样使用,

atoi(new char(digit))