0

Hi I've got follow code

HTML:

<div ng-app="myApp" ng-controller="myController">
  <input ng-model="value">
  <button ng-click="checkValue()">Check value</button>
</div>

Controller:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.value = '';

  $scope.acceptedValuesList = {
    "Items": [{
      id: 1,
      name: 'MI'
    }, {
      id: 2,
      name: 'MO'
    }, {
      id: 3,
      name: 'AB'
    }]
  };

  $scope.checkValue = function() {
    if (acceptedValuesList.Items.find((item) => item.text == $scope.value)) {
      console.log("accepted value");
    } else {
      // SANITIZE VALUE UNTIL IT'S CORRECT
    }
  }
});

When I write one of the string, which are in my array ('MI', 'MO' or 'AB') in the input and click on the button, it searches for the string in my list and if the value in the input matches with one of the three string, I print a little message in my console. This works fine. So know I would like to sanitize my value in the input, if there is also another letter. For example, when I wirte "ABx" into the input and press the button, it should remove the "x" and show "AB" in the input. How can I do this? I didn't find anything, that could help me. I hope this is clear enough. Thanks.

EDIT: MY OWN SOLUTION:

This is my solution: I write a value in my input field: input value = xyAbs;

First of all I use toUpperCase() to ignore case sensitive.

Than I loop throught my list with find() and search for my value in the current looped item.

$scope.value.toUpperCase(); // $scope.value = 'XYABS'

let item = $scope.acceptedValuesList.Items.find((item: any) => $scope.value.search(item.name) >= 0); // item = object in the list with id = 3 and name = 'AB'

After that, I check if there was an item found, so my value in the input is in one of my items in my list. Than I use substr() to get the substring in the value from the position where it starts with the length of the name. After that I get my string. If there was no item found, so my string is wrong, I clear the input:

if(item) {
    $scope.value = $scope.value.substr($scope.value.search(item.name), item.name.length); // $scope.value = 'AB'
} else {
    $scope.value = '';
}

This works fine for me, I hope it's clear enough. Thanks. :)

5
  • 3
    What if you have a string like QUWFHUIMOX ? Or, even better: What if you have a string like UQHFUMIASDMO? Commented Sep 28, 2016 at 10:06
  • @Xatenev Hi, I didn't explained it very well, it should be possible to type anything else then this three strings, so when you press 'M' or 'A' on your keyboard, it will put it into the input. After that, when you press 'I', 'O' or 'B', it also should put it into the input, but when you type any other key it shouldn't put it into the input, also when the first letter is a wrong... Commented Sep 28, 2016 at 10:40
  • @Xatenev So when you type "MIX" it should be "MI" in the input. When you type "MOR" it should be "MO" in the input. When you type "ABC", it should be "AB" in the input. And when you type "WRABC", the first to letter should be ignored, than "AB" should be in the input an the last letter "C" also should be ignored and than when you have a count of 2 letters in the input, it shouldn't be possible to type again, so "MO" should also be ignored... Commented Sep 28, 2016 at 10:43
  • 1
    if(string.search("MI") { console.log("ITS MI"); } else if(string.search("MO") { console.log("ITS MO"); } Commented Sep 28, 2016 at 11:02
  • @Xatenev Hi please look at my edited question, I found my own solution which works fine and I think it's a nice one... Cheers. Commented Sep 29, 2016 at 7:17

1 Answer 1

1

You could use watch and compare the values

$scope.$watch('value', function(newVal, oldVal) {
  var newText;
  if(newVal.indexOf("MI") > 0) {
     newText = "MI"; 
  } else if(newVal.indexOf("MO") > 0) {
     newText = "MO"; 
  } else if(newVal.indexOf("AB") > 0) {
     newText = "AB";  
  }
  $scope.value = newText;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, look please at my edited question. I found my own solution. Thanks again.
yeah, that's pretty simple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.