question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add example of checking native HTML5 form validation message

See original GitHub issue

I’m submitting a…

[ ] Bug report
[x] Content update
[ ] Process update (build, deployment, ... )

Type of bug / changes

Add an example of how to check that a form’s HTML form validation is shown when an input is invalid.

Maybe this should go in common assertions? https://docs.cypress.io/guides/references/assertions.html#Common-Assertions

Or our FAQ? I’m not sure.

it('check validation message on invalid input', () => {
  cy.get('input:invalid').should('have.length', 0)
  cy.get('[type="email"]').type('not_an_email')
  cy.get('[type="submit"]').click()
  cy.get('input:invalid').should('have.length', 1)
  cy.get('[type="email"]').then(($input) => {
    expect($input[0].validationMessage).to.eq('I expect an email!')
  })
})

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jennifer-shehanecommented, Jan 11, 2021

Sorry, the example in the FAQ is misleading since it doesn’t show the required fields for the example. I’ll update it nonetheless.

1reaction
konekoyacommented, Jul 30, 2020

Sorry for commenting on a closed issue. But I’m not sure if this is a bug or I have missed something? With the following setup:

A simple form

<form action="">
      <input type="text" id="name" name="name" required />
      <button type="submit">Submit</button>
</form>

And the spec

describe("Element", () => {
  before(() => {
    cy.visit("/");
  });

  it("it should have user value", () => {
    cy.get("input:invalid").should("have.length", 0);
  });
});

I’m getting an AssertionError of AssertionError: Timed out retrying: Too many elements found. Found ‘1’, expected ‘0’.

If I however remove the required attribute from that input, it will pass. But shouldn’t that attribute should be there and when users click on the submit button without filling the field then the input will display a native error message?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Client-side form validation - Learn web development | MDN
The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this ...
Read more >
HTML5 Form Validation Examples < HTML - The Art of Web
HTML5 form input field attributes in combination with CSS allow you to provide instant feedback on the validity of form input text, including...
Read more >
HTML5 form required attribute. Set custom validation message?
So I set the pattern attribute to check for numbers and optional decimals which gives the message, "Please match the requested format," on...
Read more >
HTML5 Form Validation With the “pattern” Attribute - Web Design
The validation process evaluates whether the input value is in the correct format before submitting it. For example, if we have an input...
Read more >
HTML5 Form Validation - Showing All Error Messages
2) In my example I start each error message with the contents of the field's <label> . This is because the messages for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found