Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Bootstrap 4 validation pseudo-classes (:valid, :invalid) with Angular 5 custom validations?

Bootstrap 4 validation styles are based on form-control:valid or form-control:invalid. Angular offers the possibility to add any custom class when a field is in error.

If i follow the spec: https://getbootstrap.com/docs/4.0/components/forms/#validation

I can also add .form-control.is-invalid class to my input element. However, using a custom validator with this strategy gives i kinda merged results; form-control is-invalid :valid

That seems to give more importance to :valid, which show my element to be shown as valid, but the related invalid-feedback is appearing okay!

I'm kinda lost.

EDIT

Here's a demo plunker: https://plnkr.co/edit/0kxGpRz3JY3ixJbYqRZr?p=preview

like image 613
JSlain Avatar asked Jan 25 '18 20:01

JSlain


People also ask

Which class does bootstrap 4 provide in order to be used only when the form control is invalid?

Here's how form validation works with Bootstrap: HTML form validation is applied via CSS's two pseudo-classes, :invalid and :valid . It applies to <input> , <select> , and <textarea> elements. Bootstrap scopes the :invalid and :valid styles to parent .was-validated class, usually applied to the <form> .

How can we make input field mandatory in bootstrap?

1. Required attribute: If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.

What is bootstrap validation?

Bootstrapping Validation is a way to predict the fit of a model to a hypothetical testing set when an explicit testing set is not available.

How do I show a field required in bootstrap?

This is pretty useful in giving a visual indication to the user when a form field is required. There will now be a nice little “*” after the label for “Required Field”.


1 Answers

Your demo is working perfectly fine , Only issue here is with class name and style


You need to remove was-validated class from the form tag and keep it as below :

class="needs-validation"

WORKING DEMO

like image 198
Vivek Doshi Avatar answered Sep 29 '22 20:09

Vivek Doshi