Show red border for all invalid fields after submitting form angularjs

后端 未结 3 814
清酒与你
清酒与你 2021-02-01 13:57

I have a form in which I have some input fields. Some of them are required fields and some are email fields.

I am using HTML5

3条回答
  •  北海茫月
    2021-02-01 14:21

    Reference article: Show red color border for invalid input fields angualrjs

    I used ng-class on all input fields.like below

    
    

    when I click on save button I am changing newEmployee.submitted value to true(you can check it in my question). So when I click on save, a class named submitted gets added to all input fields(there are some other classes initially added by angularjs).

    So now my input field contains classes like this

    class="ng-pristine ng-invalid submitted"
    

    now I am using below css code to show red border on all invalid input fields(after submitting the form)

    input.submitted.ng-invalid
    {
      border:1px solid #f00;
    }
    

    Thank you !!

    Update:

    We can add the ng-class at the form element instead of applying it to all input elements. So if the form is submitted, a new class(submitted) gets added to the form element. Then we can select all the invalid input fields using the below selector

    form.submitted .ng-invalid
    {
        border:1px solid #f00;
    }
    

提交回复
热议问题