1

Everyone, I am confused between HTML5 validation and Angular Form validation.

I want to know which validation should be used in validating a form HTML5 or angular's own form validation approach.

Please Also consider explaining:

Why and Why Not.

Which is better

Which is more secure.

1
  • 2
    It depends on your requirement. Angular validation will give you more power, on the other hand with HTML5 validation you will need to write very less and simple code. Commented Jul 24, 2018 at 11:20

1 Answer 1

3

Those are two completely different things.

On one side, you have the HTML validation. For instance :

<input type="number" name="hour" min="0" max="23">

This is a convenience validation : if you click on the arrows to increase the value, you won't go under 0 or above 23. But nothing prevents you from typing in 25.

Next, you have Angular's form validation. For instance, in the case of a reactive form

this.form = this.formBuilder.group({
  hours: ['', [Validators.max(23), Validators.min(0)]]
});

This is a technical validation : Angular checks if the form is valid at every change you make to it.

You can get the state of the form with this.form.valid and this.form.invalid.

If you rely only on HTML, you will rely on the browser's capacity to check for validation. And they aren't that advanced. Plus, your user will be able to edit the DOM and simply override your validation.

In every case when you use Angular, you should use both HTML validation and Angular's form validation. The former one for user experience, the latter one for your business rules.

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

4 Comments

I gave some examples to explain the differences. I also told you you should always use both in an Angular application.
I'm not hurt, I'm just answering 100 to 200 questions a day. I won't copy-paste what the documentation says when you can just read it by yourself ...
I have already answered your question. Now you're asking me to be your code monkey.
No problem ! If you face an issue during your implementation of form validation in Angular, don't hesitate to make a question with a minimal reproducible example. And if you consider this question to have been answered, consider marking it as resolved.

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.