TIL: That HTML forms can now ensure required fields are filled without using Javascript

in til •  8 years ago  (edited)

Today


as I was learning how to work with forms in Django, I had a form appear on my website that did this if you clicked the submit button with nothing in the field:

     There is one javascript on the page that makes the hover image on the top left but that is the only script on the page (you can check the source). That script is in fact intentionally an indicator of javascript being enabled because it can give access to malicious parties the ability to learn a lot about your web browser and use this fingerprint to associate visits to many other places on the internet as well, the so-called Evercookie.

     They say it is not as not as insecure as it used to be, but I would never, and will never trust it.

     This is what the form looks like:

<form id="checkusername" action="/sybil/check/" method="post">
  <input type='hidden' name='csrfmiddlewaretoken' value='XXXXXXXXXXXXXXX' />
  <input id="id_username" maxlength="16" name="username" type="text" required />
  <input type="submit" value="Check" />
  </form>

     The secret sauce is that required in the input field.

     This is what Django creates this with:

<form id="checkusername" action="/sybil/check/" method="post">
  {% csrf_token %}
  {{ form.message.id_for_label }} {{ form.username }}
  <input type="submit" value="Check" />
  {{ form.message.errors }}
  </form>

     I still have much to learn about building Django applications but it's so neat and flexible, even to the point of being able to transplant an environment and the website files to another computer and generate the web page for your own viewing.

     Though this feature is intended for developers you could easily use this to deploy a steem interface to user's computers as part of a python package (there is systems for building all-in-one packages that run on windows, mac, android, ios). Django only requires Python 3, python3-pip and the rest is done within the python environment.

     I'm not really that focused on web development per se but Django makes it almost seem like it would be quite interesting, but then I remember javascript and I am back on planet earth again.

     If you want to learn a web application framework, Django is a really good one. I am still having issues at times with permissions on sockets and such on a production deployment but using it on my laptop is always simple, once I learned how to get it to serve the media and static files. I should make a post to explain how to do that, it's a little hackish and I'm not sure why I needed to do it.

😎


We can't code here! This is Whale country!

Vote #1 l0k1

Go to steemit.com/~witnesses to cast your vote by typing l0k1 into the text entry at the bottom of the leaderboard.

(note, my username is spelled L Zero K One or Lima Zero Kilo One, all lower case)

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

I'm sure you already know that the required is part of HTML 5. But did you know that by using the new input types, you can also have some validation provided automatically? For example, type="email" will produce a complaint if the user doesn't enter a valid email address. The HTML would be:

<input type="email" name="email" placeholder="[email protected]">

And if you don't enter a proper address, you get an indicator similar to the one you showed that asks the user to enter a valid email address.

Finally!! Jeez... I always hated HTML forms, this makes things a bit easier..

Exchellent work..Thank you for sharing :)