Cara menggunakan REPORTVALIDITY pada JavaScript

I want to validate and show an error bubble on just one of the tens of input fields I have on a given form. Each input field has its own required and pattern attributes set, along a function that validates input on onBlur.

To do so, I call a function on onBlur that validates, sets a custom error message using setCustomValidity() and then calls reportValidity() on the form element to show the custom message on a bubble... that validates my first field alright, but when I onBlur that first field and it's valid, the next input field is showing an error bubble.

I know my problem is that reportValidity() validates the whole form and the empty required fields are indeed invalid... but how do I force it to show the error bubble on just the field I'm leaving? I'm having the hardest time figuring this out.

Just to be clear, this is what I refer when I say error bubble. I don't know it's technical name (And I would like to know it!)

Cara menggunakan REPORTVALIDITY pada JavaScript

In this article, we look at HTML form fields and the validation options offered by HTML5. We’ll also look at how these can be enhanced through the use of CSS and JavaScript.

Table of Contents

  • What is Constraint Validation?
  • Client-side vs Server-side Validation
  • HTML5 Input Fields
  • HTML Output Fields
  • Input Labels
  • Input Behaviors
  • Automatic Validation
  • Creating Custom JavaScript Inputs
  • 1. Standard controls are difficult to style
  • 2. Modern types are not supported in old browsers
  • 3. You require a new input type which has never been implemented before
  • CSS Validation Styling
  • JavaScript and the Constraint Validation API
  • Form Validation
  • Field Validation
  • Creating a Custom Form Validator
  • Form Finesse
  • How do you validate a form?
  • What is HTML5 form validation?
  • How do you check if a form is validated?
  • How do I validate a form before submitting?

What is Constraint Validation?

Every form field has a purpose. And this purpose is often governed by contraints — or the rules governing what should and shouldn’t be entered into each form field. For example, an email field will require a valid email address; a password field might require certain types of characters and have a minimum number of required characters; and a text field might have a limit on how many characters can be entered.

Modern browsers have the ability to check that these constraints are being observed by users, and can warn them when those rules have been breached. This is known as contstraint validation.

Client-side vs Server-side Validation

The majority of JavaScript code written in the early years of the language handled client-side form validation. Even today, developers spend significant time writing functions to check field values. Is this still necessary in modern browsers? Probably not. In most cases, it really depends on what you’re trying to do.

But first, here’s a big warning message:

Client-side validation is a nicety which can prevent common data-entry errors before an app wastes time and bandwidth sending data to a server. It’s not a substitute for server-side validation!

Always sanitize data server-side. Not every request will come from a browser. Even when it does, there’s no guarantee the browser validated the data. Anyone who knows how to open a browser’s developer tools can also bypass your lovingly crafted HTML and JavaScript.

HTML5 Input Fields

HTML offers: