0

I just want to validate textbox in my project in such a way so that a user could not enter any value other than characters.

1

1 Answer 1

2

Hackish? way, $watch the ng-model in your controller:

<input type="text" ng-model="myText">

Controller:

$scope.$watch('myText', function() {
   // put logic to strip out all non-character characters here
   if ($scope.myText  ... regex to look for ... ) {
      // strip out the non-characters
   }
})

Best way, use a $parser in a directive. I'm not going to repeat the already good answer provided by @pkozlowski.opensource, so here's the link: https://stackoverflow.com/a/14425022/215945

Don't try to use ng-change -- it will cause problems. See AngularJS - reset of $scope.value doesn't change value in template (random behavior)

Update: ng-pattern can also be used to define a regex that will limit what is allowed in the field. See also the "cookbook" page about forms.

Sign up to request clarification or add additional context in comments.

4 Comments

It seems u didn't understand my problem. I want that user could not enter any value other than character in the textbox. So i think here we have to use onkeypress function. Wht u say??
I believe the two solutions I provided should work. I just updated my answer with information about ng-pattern. If you can't get it to work using one of these three solutions, please post a fiddle or a plnkr of what you tried.
I already use ng-pattern for this purpose & it served me well for validating purpose but actually now i want that user couldn't even type anything in textbox apart from characters. Could you please help me with this??
See stackoverflow.com/a/15556249/215945 where a directive was written to prevent non-numeric characters from being entered. Just change the regex in the directive to allow only characters.

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.