rish rish - 4 months ago 10
AngularJS Question

Starting with Number and Ending with Character in Angular JS

I just started learning AngularJs So sorry for dump question. I have requirement to validate the field which will start with number and end with 'M' character eg:- 10M, 5M, 200M etc. please any one help me how to do it in angular js.

Answer

You can use ng-pattern in your input field e.g.

<input type="text" ng-model="model" id="input" name="input" ng-pattern="regex" />

and in your controller define regex as

$scope.regex = '\\d+M';

And you can check in your html, if input is valid or not like this:

input valid? = <code>{{form.input.$valid}}</code>

This will work well with ng-messages directive for form validation.

Read More.