I am creating a member registration form where he enters his membership card number. The membership card number is a 19 digit number which starts with 6 4's and then followed by 13 digits.Now the user has the option to enter either last 13 digits alone or he can even enter the complete 19 digits. When he enters complete 19 digits on the blur event i trim the 1st 6 digits which leaves the last 13 digits. My code works fine for all scenarios but something weird happens when error happens in 1 particular scenario.
Working Scenarios are
- If the user types anything less then 13 digits or more then 19 digits i have a regex which validates the user input and shows the error message.
- If the user just types 13 digits, its fine
- If user types perfect 19 digits but his starting 6 digits are not 4's then error is diplayed.
Failed Scenario
- But when user types any digit >13 and <19 error message is triggered but my input field becomes empty, i.e. whatever user has typed just vanishes. Is this angular library issue
Please find the code below-
HTML
<input type="text" ng-model="user.CardNumber" ng-blur="trimCardNumber()" ng-required="true" ng-pattern="/^[0-9]{13,19}$/" name="CardNumber">
Controller.JS
$scope.trimCardNumber = function () {
if (($scope.user.CardNumber).indexOf("444444") > -1) {
$scope.user.CardNumber = ($scope.user.CardNumber).replace("444444", "");
$scope._ServerForm.CardNumber.$invalid = false;
return true;
}
else if ($scope.user.CardNumber.length != 13) {
$scope._ServerForm.CardNumber.$invalid = true;
}
else {
$scope._ServerForm.CardNumber.$invalid = false;
}
}
I have created a JS fiddle for the issue - http://jsfiddle.net/achyut/x69hZ/1/
Steps to reproduce -
- Type 444444123456789012 and blur to see the issue