1

Consider this input control:

<input ng-model="vm.loanAmount" ng-min="1" type="text" name="LoanAmount" id="LoanAmount" required>

Consider this validation:

<ng-messages for="myForm.$dirty && myForm.LoanAmount.$error" multiple>
    <ng-message when="required">
        <p>An amount is required.</p>
    </ng-message>
    <ng-message when="number">
        <p>The amount must be a valid number.</p>
    </ng-message>
    <ng-message when="min">
        <p>The amount must be greater than 0.</p>
    </ng-message>
</ng-messages>

I need this field as a text type field, but want to use ng-messages module to validate it to take numbers only. How is this possible without changing the field type to number?

At the moment, the only message that successfully fires is the required parameter. I'd like to allow numbers only, over the value of 0.

Thanks.

3
  • 2
    Without changing the field type to number, you'll have to write your own directive. I did such a thing a year or two ago, b/c the users wanted to insert commas in large numbers. You'll basically have to validate the number-ness, min, max, etc yourself. Commented May 26, 2015 at 16:56
  • alternative to creating a directive (which is probably an elegant solution no doubt), do you think you can also consider the ng-pattern attribute and eval the input text string and regex for validation in the controller? something like this: http://stackoverflow.com/questions/18900308/angularjs-dynamic-ng-pattern-validation Commented May 27, 2015 at 1:33
  • Possible duplicate of Using ngMessage for input type number Commented Mar 12, 2016 at 6:45

0

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.