Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML

Kieran Barker
Kieran Barker
15,004 Points

Is automatic HTML form validation sufficient?

Imagine I had the following HTML. Please pay particular attention to the <required> attribute on the <input> element.

<form action="/action_page_post.php" method="post">
  <input type="text" name="fname" required>
  <input type="submit" value="Submit">
</form>

Would this type of automatic form validation be sufficient on a simple form, or is JavaScript/PHP/some other kind of validation definitely necessary?

1 Answer

Ari Misha
Ari Misha
19,274 Points

Hiya there! The required attribute only ensures that this field is required and shouldnt be empty. Forms in back-end is like an another different world, where you store the details filled by user in your database , and also sanitize the entries whilst validating 'em as well. And password gets encrypted and added salt in order to store 'em in your database and a session is created if this is a sign up or sign in form. So yeah a lots of stuff happens in back-end when the app processes a form. But HTML only take cares of your front-end or markup on your webpage.

~ Ari

Kieran Barker
Kieran Barker
15,004 Points

Thanks for your input, Ari! No pun intended. The thing is, I've noticed that HTML can check if some input fields are formatted correctly. For example, when I use the following HTML code for an email input field:

<form action="newsletter.html" method="post" id="newsletter_form">
  <label for="newsletter">Mailing List</label><br>
  <input type="email" id="newsletter" name="visitor_newsletter" placeholder="Enter your email address..." required><br>
  <button type="submit">Sign Up</button>
</form>

Then the browser returns the following error message if the entered email address isn't formatted correctly:

E-mail input error

What do you think? :-)

Ari Misha
Ari Misha
19,274 Points

Kieran Barker of course HTML can, but in real world applications , you'll hardly see this kinda validation. And its just very basic validation but yeah its still validation and its still useful. Plus you already gave it a type to email , so yeah behind the scenes it performs basic validation to check if the entries are correct or not. (: