我有一个文本框,其中我正在获取长度为 20 的文本类型的输入,我有一个场景,其中我必须评估从客户端输入的整个字符串是否不为零或任何特殊字符
例如:00000000000,00000,00,00000,00000/-/--,/-/-----////,aBadf-018---///sdf484,AA////---000
以上所有输入均无效,字符串可以是 2 到 20 之间的任意长度,
我限制用户输入除连字符和破折号之外的其他特殊字符, 此处使用的正则表达式使连字符和破折号以外的值无效,如下所示:
"/[^a-zA-Z0-9/-]/g "
<input type="text" name="consumer_number" maxlength="25" ng-disabled="!form_data[selectedTab].electricity.check" alpha-numeric-with-slash-and-hyphen data-ng-model="form_data[selectedTab].electricity.unique_no" class="input-field" data-ng-blur="validateElecConsumer(selectedTab)" ng-class="{true:'invalid-field',false:''}[form_data[selectedTab].electricity.check && form_data[selectedTab].invalid.electricity]" required />
现在我关心的是如何预先显示一条消息,表明用户提供的任何输入都是无效的。
最佳答案
这可能有帮助:
var app = angular.module("app", []);
app.controller("MyController", function($scope){
$scope.inputtext = "";
$scope.onSubmit = function() {
var val = $scope.inputtext;
if (val.length < 2
|| val.length > 20
|| !val.match(/[^a-zA-Z0-9/-]/g)) {
alert("Unacceptable");
} else {
alert("you pass");
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyController">
<input maxlength="20"
id="inputtext"
ng-model="inputtext" type="text"/>
<button id="submitbutton" ng-click="onSubmit()" type="text">submit</button>
</div>
关于javascript - 检查文本框中的整个字符串值是否为零(0)或特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42373903/