0

I would like to know how can i display an error message below the input field using angular js. If the field is empty i want to display "This field is required". Also how to check if the field name starts with alphanumeric values like (\?/%*:|"<.>) i need to display the error "The field name should not start with alphanumeric values" below that input field. // code goes here

<div ng-controller="ModalController">

                    <form novalidate name="inputForm" class="form-horizontal">
                <div class="row">
                    <div class="col-md-12">
                        <cp-input name="username" ng-model="user.name"
                            label="{{'formlabels.reponame' | translate}}" required="true"
                             error-state="right">
                        </cp-input>
                    </div>
                </div>
                <div style="color:red" ng-show="inputForm.username.$error.required">First name is required</div><br>
                </form>
                <!-- <a href="javascript:void(0)" ng-click="findUsers()"> Show Confirm </a> -->
                <button type="button" class="btn btn-primary" data-dismiss="modal"
                    data-ng-click="submitData()">Add</button>
            </div>

2 Answers 2

1

It would be a good idea to use ngMessages directive and ngPattern - take a look at the doc link with example

 <form name="myForm">
   <label>
   Enter your name:
   <input type="text"
       name="myName"
       ng-model="name"
       ng-minlength="5"
       ng-maxlength="20"
       ng-pattern="^[^\w+$]"
       required />
  </label>
  <pre>myForm.myName.$error = {{ myForm.myName.$error | json }}</pre>

  <div ng-messages="myForm.myName.$error" style="color:maroon" role="alert">
    <div ng-message="required">You did not enter a field</div>
    <div ng-message="minlength">Your field is too short</div>
    <div ng-message="maxlength">Your field is too long</div>
 </div>
 </form>

ref https://docs.angularjs.org/api/ngMessages/directive/ngMessages

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

Comments

1

ng-messages will help you..Try this

<input ng-model="typeText" id="Sample" name="Sample" type="text" ng-pattern="^$|^[A-Za-z0-9]+" required/>
 or ng-pattern="/^[a-zA-Z\s]*$/" 
        <div ng-messages="formname.Sample.$error">
          <div ng-message="pattern">only characters are allowed</div>
          <div ng-message="required">field required</div>
        </div>

2 Comments

the above regex is not working according to my requirement. My requirement is, in the input name the first character should not start with special character. If the special character is in between it should allow.
Then choose custom directive, in that use preventDefault() on keypress, change, paste. Compare model length is 0 or get first letter of model. If have special character to deny.

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.