Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted 4 months ago

Contact Form Using JS

accessibility, lighthouse, pure-css, solid-js
Adelabu Mobolaji•440
@Bolazcoding
A solution to the Contact form challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

I'm just happy about completing this project. it was challenging

What challenges did you encounter, and how did you overcome them?

Yeah, I had issues with the form validation Spent hours trying to figure out the best solution

What specific areas of your project would you like help with?

JavaScript

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Awais Ahmed•180
    @its-awais
    Posted 4 months ago

    look, don't need to define global variable, because you want to to check validation when you click the form, so use let variable in form validation, 2)look man you make your code more complicated, let me show you an easy way to do it."use strict";

    const firstName = document.querySelector("#fname");

    const lastName = document.querySelector("#lname");

    const email = document.querySelector("#email");

    const queries = document.querySelectorAll(".query-radio");

    const email_error = document.querySelector("#email-error");

    const error_query = document.querySelector("#dispQueryError");

    const error_term = document.querySelector("#dispTermError");

    const error_message = document.querySelectorAll(".error-msg");

    const formButton = document.querySelector(".btn");

    const firstNameChecking = () => { let firstNameValue = firstName.value.trim();

    let firstNameRegex = /^[a-zA-Z]{2,}$/;

    if (firstNameValue === "") { error_message[0].textContent = "first name is required"; } else if (!firstNameRegex.test(firstNameValue)) {

    error_message[0].textContent =
      "First name must contain only letter and be at least 2 character";
    

    } else {

    error_message[0].textContent = "";
    

    } };

    const emailChecking = () => {

    let emailAddress = email.value.trim();

    let emailRegex = /^[a-zA-Z-9._%+-]+@[a-zA-Z]{3,}$/;

    if (emailAddress === "") { error_message[2].textContent = "Please enter your email"; } else if (!emailRegex.test(emailAddress)) { error_message[2].textContent = "please enter a valid email address";

    } else { email_error[2].textContent = ""; } };

    const queriesChecking = () => { let isChecked = false; queries.forEach((query) => {

    if (query.checked) {
      isChecked = true;
    }
    

    });

    if (!isChecked) { error_query.textContent = "Please select a query type";

    } else { error_query.textContent = ""; } };

    const formValidation = (e) => { e.preventDefault(); firstNameChecking(); emailChecking(); queriesChecking(); };

    formButton.addEventListener("click", formValidation); this easy code, explaning
    1)when user click the button then i use formValidation look i did not use "prentisis()" because i don't want that it excute immediatly when script load i want that when button clicked 2)then it goes in formValidation and check when cliked now i use to call the function with prentisis because, i want that when script load then excute so, firstName() check the regex and match with input that user put if user put the value is empty he says please enter a value, "test" can give you two value true and false. 3) why i use error_message[2] because you use this error message everywhere in html so 0 index there is "firstName" first index there is "lastName" and then third index which is 2 its email. 4)checking queries so we need to check whethere user click the value or not, so first we isChecked = false; because when first time user came he don't now, so that's why we use false. And then we use forEach because there is two queries "genral" and second one is "support" so forEach and if(isChecked) if user click the query then make isCheked to true and if isChecked its mean if user does not click then do that.

    if you have any other question plz ask

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