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.

7 thoughts on “17 JavaScript Form Validation Snippets

  1. I hate Javascript, but I can see that you didn't check if trim(input) == empty.

    Trim doesn't exist in JavaScript, so searching for a solution I crossed with a script that implements this. Here it is:

    function trim(c){
    for(i=0;i=0;i=c.length-1){
    if(c.charAt(i)==" ") c=c.substring(0,i);
    else break;
    }
    if (c=='') return false;
    else return true;
    }

    It's dirty, it can be optimized, minified and obfuscated, but it does his job.

    Greetings !!

  2. lol, after checking, the above function is wrong... It should be:
    function trim(c){
    for(i=0;i=0;i=c.length-1){
    if(c.charAt(i) == " ") c=c.substring(0,i);
    else break;
    }
    if(c=='') return false;
    else return true;
    }

    Greetings !!

  3. @unreal4: that's a lot of comment, haha..

    i purposely didn't trim when i validate actually 🙂

    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 😀

  4. @Clay: I couldn't edit xD Sorry for that.

    Nice compilation of trim functions there! I've included my demo in the benchmark script Steven's website and I realized it was really far from perfect.

    It scored at third place... from the bottom xD

    trim8: 3739ms (length: 27654) --> slowest
    trim11: 37ms (length: 27654) --> fastest
    trim13: 3051ms (length: 27657) --> mine xD

    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

Comments are closed.