0

Hi i'm new to angularjs development and i'm struggling with form validations.

I have a input field that allows maximum of 8 digits. I want to show a error message if 8 digits are NOT given. Also the error message should disapear when 8 digits are given.

How can i do this?

My field

<input maxlength="8" ng-model="customer.customerId" class="form-control" id="id_customer_select_id" placeholder="Enter existing customer id" ng-change="selectCustomer()" name="customer_id" ng-value="customerId" required="required" > 

Thanks in advance.

2
  • Try ng-maxlength and ng-minlength. Commented Sep 15, 2016 at 7:07
  • i tried ng-maxlength but it did not work. Only maxlength worked for me. Commented Sep 15, 2016 at 7:14

2 Answers 2

2

It would a better option to use <form> </form> for the validation

also use ng-messages to display your validation error message

Given below is the example how to use validation for your question:

Demo Example

<input type="text" placeholder="Number" name="number" ng-model="mobile" ng-maxlength="8" required /> <div ng-messages="formName.number.$error" item- style="color: red"> <div ng-message="required">Max number entered!! </div> </div> 
Sign up to request clarification or add additional context in comments.

Comments

1

try this

<div ng-show="formName.elementName.$invalid && formName.elementName.$dirty"> <span ng-show="formName.elementName.$error.required"> <div style="font: normal 20px courier">This field should be 8 numbers long</div> </span> </div> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.