Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 3 years ago

Javascript form validation

geoffjecrois•390
@geoffreyhach
A solution to the Intro component with sign-up form challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


About the javascript form validation, is it the best practice to create a new function for every input like I did?

-I couldn't find how to add the svg icon in the input field, any suggestion? I tried to use a pseudo element on the error message span, but i couldn't position it like I wanted. EDIT : with the label elements that i forgot right?

-I did a desktop first version but I should have start with mobile I guess.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Vanza Setia•27,715
    @vanzasetia
    Posted over 3 years ago

    Hello there! 👋

    When I did this challenge, I made a separate function to validate the user input value. However, I only created one main function that handled the submit event.

    const validateUserInputs = event => {
      /** 
        * If the user inputs are not valid then:
        * - Prevent the form from submitting
        * - Show error messages
        *
        * P.S. This is just a brief overview
        */
    };
    
    form.addEventListener("submit", validateUserInputs)
    

    So, my recommendation is for every input, it has its own function to validate the value. In this case, there will be validatePassword(), validateEmail(), and validateName() for both name.

    For the SVG icon, you can use background-image on the input element when the input is in an invalid state.

    Yes, I highly recommend writing the styling using the mobile-first approach. It often leads to shorter and better performance code. Also, mobile users won't be required to process all of the desktop styles.

    There are some areas that need to be fixed.

    • Very important, don't make the value of the input disappear every time the users focus on the input element.
    • The label should contain a text that explains what the input is about (it should not be empty).
    • input with type="submit" is a browser legacy. I recommend using button with type="submit" instead.
    • b has been deprecated. Use strong tag instead.
    • "Try it free 7 days then $20/mo. thereafter", it should not be a button. It's just a paragraph element.
    • Remove the unnecessary white space on the HTML file. It increases the file size and possibly makes the HTML code harder to read.
    • I recommend using CSS classes instead of inline CSS through JavaScript. Just like having an inline styling on the HTML should be avoided, the same principle applies in JavaScript as well.

    I highly suggest writing your code with consistent style (e.g. the indentation, quotes, whitespace, etc). If you write your code with consistent style, it will make it easier to read for everyone (including your future self).

    I hope you find this information beneficial. Happy coding! 😄

    Marked as helpful
  • Otávio•230
    @Otavio-Cipriano
    Posted over 3 years ago

    Hey there, good job completing this challenge.

    Some advices

    • Try to give name to all input on the html

    • During the submit event you don't need to access the document again to select the input inside the form, you could select them using that "const myForm" and accessing the input by name

    example:

    let email = myForm['email']

    • You could also use all that validation code inside the same eventListener function, because is the same event to the same element, there's no reason to separate that.

    I hope this helps and it didn't sound rude or weird.

    Cheers!

    Happy coding 👍

    Marked as helpful

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub