-3

I need to find ',' from input string of text-area that i have and on based on result i need to restrict input string or give alert to user. i am new with angular and not sure how to achieve this functionality.

Here's my code that i tried:

<div class="text-area-container">
    <textarea id="txt_1" class="txt_1-box" rows="2" data-ng-model="MyTextAreaValue"></textarea>
</div>

Angular Code:

$scope.myfun= function($event) {
    var myStr = $scope.MyTextAreaValue;       
   // Here I am not sure after this  what i have to do?
};

Please note I need to check that if there are max 3 comma it gives you alert or stop user to add other comma to string.

Thanks in Advance!!

Hardy Mathew
  • 684
  • 1
  • 6
  • 22
  • 1
    use javascript string methods to check with ... not hard to research those – charlietfl Oct 13 '15 at 22:57
  • I know how to do that using JAVASCRIPT but i need to know with angular stuff if it is and Please note I need to check that if there are max 3 comma it gives you alert or stop user to add other comma – Hardy Mathew Oct 13 '15 at 23:05
  • bind a key event handler to it ... all the angular event handler directives are listed in the angular api docs menu. If you know how to check why wasn't that shown in your question? – charlietfl Oct 13 '15 at 23:09

2 Answers2

0
if(
  (myStr.match(/,/g) || []).length >= 3
){
  // stop

}

credit: Count the number of occurrences of a character in a string in Javascript

Community
  • 1
  • 1
Benjaco
  • 303
  • 3
  • 11
0

ngMask does a good job of setting a mask to an input field, if that helps.

Mo Binni
  • 265
  • 1
  • 12