17 JavaScript Form Validation Snippets

Last week i wrote 25 form validation for PHP. I find useful to have more layer of defend as JavaScript might malfunction or disable and cause your validation to fail but the form is still being passed on to our server. Nonetheless, JavaScript validation does provide certain level of restriction and protection for any web application. Especially when web is evolving quickly in this period of time. We are required to validate any data from our user to prevent any harm that can cause damage to our application and businesses. These JavaScript form validation snippets are needed in every form validation and repeating searching for them is unnecessary and slow down development processes. Therefore, you might want to bookmark these snippets for your future needs

Email Validation

Email validation is the most basic validation that any web application would have.

The reason is simple. Something that user mistakenly type wrongly shouldn't be corrected by our script especially on the username and password validation since they are the most sensitive part of a system that hacker would like to break. And helping by trimming our value should be avoided. (this should be the same with PHP side)

Nonetheless, you are right on other fields that trim should exist. But i will leave it optional for everyone since we will still have to trim it on the server side scripting as the data still wasn't trimmed when it is passed to server. Hence, i tried to avoid additional code and logic into the JavaScript code 🙂

Steven have a multiple version of trim that i always refer to since there are many participates on his many trim version.

Once again! Thanks for the participation! I learn a lot from you lately 😀

However, the worst part is that my function wasn't reliable. The string length isn't the same as the others are. That is something to worry about! So if you're reading this, DON'T use my previously posted trim solution!

Anyway, I've learned a lot also! Like I said, I just hate JavaScript, so this article goes directly into my favs! xD