Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look into using Constraint Validation for Nebula form validation [🟩 fully supported now] #1679

Open
chrisblakley opened this issue May 6, 2018 · 2 comments
Labels
Frontend (Script) Related to the client-side JavaScript. Plugin / Library / API For third-party resources such as WordPress plugins, external APIs, and other libraries.
Milestone

Comments

@chrisblakley
Copy link
Owner

chrisblakley commented May 6, 2018

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation#Complex_constraints_using_HTML5_Constraint_API

Currently Nebula uses data attributes to manage real-time validation, but as browser support grows, consider switching over to (or also using) setCustomValidity() which will add custom messages to HTML5 validation messages.

Not supported in IE11, but works in all other browsers. Could implement as a progressive enhancement. https://caniuse.com/#feat=constraint-validation

@chrisblakley chrisblakley added Plugin / Library / API For third-party resources such as WordPress plugins, external APIs, and other libraries. Frontend (Script) Related to the client-side JavaScript. labels May 6, 2018
@chrisblakley chrisblakley added this to the 6.0 Ant milestone May 6, 2018
@chrisblakley chrisblakley modified the milestones: 6.0 Ant, 7.0 TBD May 19, 2018
@chrisblakley
Copy link
Owner Author

From the documentation on HTML5 Constraint API:

function checkZIP() {
  // For each country, defines the pattern that the ZIP has to follow
  var constraints = {
    ch : [ '^(CH-)?\\d{4}$', "Switzerland ZIPs must have exactly 4 digits: e.g. CH-1950 or 1950" ],
    fr : [ '^(F-)?\\d{5}$' , "France ZIPs must have exactly 5 digits: e.g. F-75012 or 75012" ],
    de : [ '^(D-)?\\d{5}$' , "Germany ZIPs must have exactly 5 digits: e.g. D-12345 or 12345" ],
    nl : [ '^(NL-)?\\d{4}\\s*([A-RT-Z][A-Z]|S[BCE-RT-Z])$',
                    "Nederland ZIPs must have exactly 4 digits, followed by 2 letters except SA, SD and SS" ]
  };
  
  // Read the country id
  var country = document.getElementById("Country").value;

  // Get the NPA field
  var ZIPField = document.getElementById("ZIP");

  // Build the constraint checker
  var constraint = new RegExp(constraints[country][0], "");
    console.log(constraint);


  // Check it!
  if (constraint.test(ZIPField.value)) {
    // The ZIP follows the constraint, we use the ConstraintAPI to tell it
    ZIPField.setCustomValidity("");
  }
  else {
    // The ZIP doesn't follow the constraint, we use the ConstraintAPI to
    // give a message about the format required for this country
    ZIPField.setCustomValidity(constraints[country][1]);
  }
}

@chrisblakley
Copy link
Owner Author

This is now fully supported.

Screen Shot 2023-07-24 at 5 35 22 PM

@chrisblakley chrisblakley changed the title Look into using Constraint Validation for Nebula form validation Look into using Constraint Validation for Nebula form validation (🟩 fully supported now) Jul 24, 2023
@chrisblakley chrisblakley modified the milestones: 12.0 Flame, 13.0 Bubble May 1, 2024
@chrisblakley chrisblakley changed the title Look into using Constraint Validation for Nebula form validation (🟩 fully supported now) Look into using Constraint Validation for Nebula form validation [🟩 fully supported now] Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frontend (Script) Related to the client-side JavaScript. Plugin / Library / API For third-party resources such as WordPress plugins, external APIs, and other libraries.
Projects
None yet
Development

No branches or pull requests

1 participant