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.

Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140
  • 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. – Sunil D. May 26 '15 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](http://stackoverflow.com/questions/18900308/angularjs-dynamic-ng-pattern-validation) – Shehryar Abbasi May 27 '15 at 01:33
  • Possible duplicate of [Using ngMessage for input type number](http://stackoverflow.com/questions/24407720/using-ngmessage-for-input-type-number) – Paul Sweatte Mar 12 '16 at 06:45

0 Answers0