如何使用Angular.js禁止只允许字符进入输入字段

How to disallow only characters into input field using Angular.js

本文关键字:字符 字段 输入 许字符 Angular 何使用 js 禁止      更新时间:2023-09-26

我不需要只允许字符进入我的输入字段,并且想使用Angular.js检查验证。我的代码如下:

<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right oditek-form" style="width:180px">Longitude:</span>
<div ng-class="{ 'myError': billdata.longitude.$touched && billdata.longitude.$invalid }">
<input type="text" name="longitude" id="longitude" class="form-control oditek-form" placeholder="Add Longitude coordinate" ng-model="longitude"  ng-pattern="/^[0-9]+([,.][0-9]+)?$/" ng-keypress="clearField('businessno');" >
</div>
<span class="input-group-btn">
<a ui-sref="dashboard.customer.view" class="btn btn-success" style="height:30px;" title="Click Here" ng-click="getLongValue();"><i class="fa fa-map-marker" aria-hidden="true"></i></a>
 </span>
</div>

在上面的代码中,我也设置了一些正则表达式到ng-pattern,但它不允许像-12.345,+2.345 etc这样的数字。这里我需要这个字段只允许数字(' I -1,2…9'),特殊字符,如(only +, -, . etc)。

ng-pattern的模式应该匹配:

ng-pattern="/^[0-9,'.'-'+]+$/"

可能ng-pattern = "/0 - 9, ' W/g"