2016-07-02 86 views
-1

你好新手到角js? 一旦用戶輸入十進制值或顯示錯誤的字符串js中的字符串,如何只允許texbox中的數字?如何只允許angularjs中的文本框中的數字?

<div class="col-md-6 col-sm-6"> 
                 <label class="col-md-5 col-sm-5 pad-top5 pad-lft-no">Min <span class="error">*</span></label> 
                 <input positive type="number" class="col-md-7 col-sm-7 cls_input_inherit numberinput" min="0" ng-maxlength="3" id="age_min" name="age_min" ng-model="attributes.age_min" required/> 
                 <label for="age_min" ng-show="submittab2 && attributesForm2.age_min.$error.required" class="error">{{formValidation.required}}</label>               
                 <label for="age_min" ng-show="submittab2 && !attributesForm2.age_min.$error.positive && attributesForm2.age_min.$error.maxlength" class="error"> {{formValidation.monthMaxChar}} </label> 
                 <label for="age_min" ng-show="submittab2 && !attributesForm2.age_min.$error.positive && !attributesForm2.age_min.$error.maxlength && attributesForm2.age_min.$error.min" class="error">{{formValidation.minMax}}</label> 
                 <label for="age_min" ng-show="submittab2 && !attributesForm2.age_min.$error.positive && !attributesForm2.age_min.$error.maxlength && attributesForm2.age_min.$error.number" class="error">{{formValidation.errorNumber}}</label> 
                 <label for="age_min" ng-show="submittab2 && attributesForm2.age_min.$error.positive" class="error">{{formValidation.minpositive}}</label> 
                </div> 

我試過上面的代碼,但是輸入十進制值時不顯示錯誤?怎麼解決?

+0

找到答案:http://stackoverflow.com/questions/32777184/html-input-for-positive-whole-numbers-only-type-number – varit05

+0

嘗試使用的輸入類型爲'number'和'模式=「^ [0-9] * $」'屬性 –

+0

可能的重複http://stackoverflow.com/questions/8808590/html5-number-input-type-that-takes-only-integers –

回答

0

您需要在輸入字段中添加ng-pattern =「/ ^(\ d)+ $ /」。

<div class="col-md-6 col-sm-6"> 
<label class="col-md-5 col-sm-5 pad-top5 pad-lft-no">Min <span class="error">*</span></label> 
                <input positive type="number" class="col-md-7 col-sm-7 cls_input_inherit numberinput" min="0" ng-maxlength="3" id="age_min" name="age_min" ng-model="attributes.age_min" ng-pattern="/^(\d)+$/" required/> 
                <label for="age_min" ng-show="submittab2 && attributesForm2.age_min.$error.required && attributesForm2.age_min.$touched && attributesForm2.age_min.$invalid" class="error">{{formValidation.required}}</label>               
                <label for="age_min" ng-show="submittab2 && !attributesForm2.age_min.$error.positive && attributesForm2.age_min.$error.maxlength" class="error"> {{formValidation.monthMaxChar}} </label> 
                <label for="age_min" ng-show="submittab2 && !attributesForm2.age_min.$error.positive && !attributesForm2.age_min.$error.maxlength && attributesForm2.age_min.$error.min" class="error">{{formValidation.minMax}}</label> 
                <label for="age_min" ng-show="submittab2 && !attributesForm2.age_min.$error.positive && !attributesForm2.age_min.$error.maxlength && attributesForm2.age_min.$error.number" class="error">{{formValidation.errorNumber}}</label> 
                <label for="age_min" ng-show="submittab2 && attributesForm2.age_min.$error.positive" class="error">{{formValidation.minpositive}}</label> 
               </div> 
+0

雅嬋ged就像上面的代碼一樣,在給定十進制值時不會顯示任何錯誤,也累了step =「1」 –

+0

你需要在這裏面用lable ng-show =「submittab2 && attributesForm2.age_min。$ error.required && attributesForm2.age_min。$感動&& attributesForm2.age_min。$無效「 –

+0

Thanq這麼多它的工作 –

相關問題