简体   繁体   中英

How to use user input value as part of regular expression for other field validation in AngularJS

I have 2 fields with their respective ng-model fields. I want to use 1st field value in 2nd field's ng-pattern expression. Eg

EmployeeID and EmployeeFile fields. EmployeeID is mapped to $scope.empId . EmployeeFile is mapped to $scope.file . For validating file input, the pattern should be like ^.{{$scope.empId}}-\\d{3}\\.(xls|xlsx)$ .

Is is possible to have one model affect another regex validator for another field in AngularJS?

I've got something like

<input type="text" ng-model="emp.empId"> 
<input type="text" ng-model="emp.empFile" ng-pattern=".....">

What should I fill in the ng-pattern if I want the value to be {{empId}}.xls ?

first you should replace the data with empId and check with reg-exp and again concat the matched reg-exp content and dynamic empId and check

  inputValue=scope.file.replace($scope.empid,'');
   regexp = ^.-\d{3}.(xls|xlsx)$"; //without empid 
     var transformedInput = inputValue.match(REGEXP);
        if (transformedInput==inputValue) {
          validfile = $scope.empid+inputValue;

  }

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