0

I am New to angularJS. I have 6 Input tags without form . that use click on button. I want to make require;

here is code:

     <td><input class="form-control" ng-model="contact.tradeMark" required="string" placeholder="Trade Mark"  ></td>
     <td><input class="form-control" ng-model="contact.applicationNo" placeholder="Application No"></td>
     <td><input class="form-control" ng-model="contact.class" placeholder="Class"></td>
     <td><input class="form-control" ng-model="contact.status" placeholder="Status"></td>
     <td><input class="form-control" ng-model="contact.client" placeholder="Client"></td>
     <td><input class="form-control" ng-model="contact.applicant" placeholder="Applicant"></td>
     <td><input class="form-control" ng-model="contact.remark" placeholder="Remark"></td>

     <td><button class="btn btn-primary" ng-click="addContact()">Add Contact</button></td>
     <td><button class="btn btn-info" ng-click="update()">Update</button>&nbsp;&nbsp;<button class="btn btn-info" ng-click="deselect()">Clear</button></td>

Thanks in Advance

1 Answer 1

1

There are ways, by which one can add the required attribute in the input field:

  1. Adding required attribute directly -- This is the original HTML way

Code:

<input class="form-control" ng-model="contact.tradeMark" required placeholder="Trade Mark" />
  1. Adding ng-required="true/false" attribute in the field -- This is AngularJS way.

Code:

<input class="form-control" ng-model="contact.tradeMark" ng-required="true" placeholder="Trade Mark" />
  1. Adding ng-required="<some_boolean_condition>".

Code:

<input class="form-control" ng-model="contact.tradeMark" ng-required="isRequired" placeholder="Trade Mark" />

where isRequired is a model binded between a view and the controller.

UPDATE:

Refer link: here

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

5 Comments

1st and 2nd solution is not working. or in 3rd what i need to change in controller
I don't know whether you are making use of HTML elements properly or not, for required to make use one must include form. All the three solutions provided above are working fine. I have updated the answer by adding demo link. Do visit it.
for 3rd, the model value should be Boolean assigned/computed in the controller.
If i use form then i need to change my button too. and there is two button both work is different. If i put inside the form then , that is dificult for me
One can have multiple buttons inside a form but button of type="submit" must be one. Try changing your code, otherwise required is of no use without form.

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.