简体   繁体   中英

Check if the entire String value in Textbox is zero(0) or of special characters

I have a Textbox in which i'm getting the input of type text having the length 20, I have a scenario in which i have to evaluate whether the entire string input from the client is not zero or any special characters

eg: 00000000000,00000,00,00000,00000/-/--,/-/-----////,aBadf-018---///sdf484,AA///---000 all these above inputs are invalid, the string could be of any length in between 2 to 20,

i have restricted the user to input other special characters other than hyphen and dash, regex used over here to invalidate the value other than hyphen and dash are as follows :

" /[^a-zA-Z0-9/-]/g "

<input type="text" name="consumer_number" maxlength="25" ng-disabled="!form_data[selectedTab].electrici‌​ty.check" alpha-numeric-with-slash-and-hyphen data-ng-model="form_data[selectedTab].electric‌​ity.unique_no" class="input-field" data-ng-blur="validateElecConsumer(selectedTab‌​)" ng-class="{true:'invalid-field',false:''}[form‌​_data[selectedTab].e‌​lectricity.check && form_data[selectedTab].invalid.electricity]" required /> 

Now my concern is how could i display a message right upfront that whatever the input the user have provided is invalid.

This may help:

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM