0

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.

1
  • Add some relevant code. Commented Jul 15, 2016 at 7:05

3 Answers 3

2

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.

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

Comments

0

You can use the ngPattern directive to validate by matching a regular expression:

Angular docs: ngPattern directive

The pattern you need is \d+M (a sequence of 1..n digits, followed by an 'M'), you can try it out on the page I linked.

Comments

0

AngularJS provide a usefull directive ng-pattern that you can apply on an input.

For your problem, you can use this code :

<input type="text" ng-pattern="/^\d+[M]$/" ng-model="model" id="input"  />

Explanation

/d means digits only

+ means at least one

[M] means M characters only

Comments

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.