SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,343
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)

    What is the point of client side form validation?

    Hi,

    I'm reading Simply JavaScript and have come to the bit about client side form validation.
    Supposing I have a form where the user can submit their name and email address. If I check that the user input is correct using JavaScript and someone comes along with JavaScript turned off, they can effectively submit whatever they like.
    This is acknowledged in Simply JavaScript, where the author states that server side validation is in fact a must.
    So my question, what is the point of client side form validation if you have to do the same work again on the server?
    Am I missing something?
    Are there any cases where client side validation is the preferred choice (apart from speed)?

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Performance. Catching errors client-side will be much more efficient, since it doesn't require a round trip to the server. Less server load, faster feedback to the user.

    It doesn't replace server-side validation, of course, but it enhances the user experience.
    Birnam wood is come to Dunsinane

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,607
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Server side validation is for the site owner. It makes sure everything has been entered correctly.

    Client side validation using JavaScript is to make things more convenient for the person filling out the form. It tells them if they entered something wrong before they fill out and submit the whole form so that they can fix it before submitting it to the server.

    Visitors with JavaScript disabled can still submit the form but they have to wait until after they fill it out and submit it to find out if they got anything wrong.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,343
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Thanks for the replies.
    So, if I understand things correctly then, client side validation is a nice-to-have enhancement of the user's experience of your site, but can never be relied on if you have to be absolutely sure that certain fields have been filled out.

    Do you know if many people bother to have both client side and server side validation?

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You always have server side validation.
    I often use that code to shape any client side validation, so it's not really two completely different sets of code to come up with.

    You can also track server validation errors-
    Figure what causes the user mistakes and you can improve the layout and instructions on the form to mitigate them.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Pullo View Post
    Thanks for the replies.
    So, if I understand things correctly then, client side validation is a nice-to-have enhancement of the user's experience of your site, but can never be relied on if you have to be absolutely sure that certain fields have been filled out.
    Got it in one, well done. That is the fundamental dichotomy of JavaScript. It's great to use, but you cannot rely on it actually being used.

    Quote Originally Posted by Pullo View Post
    Do you know if many people bother to have both client side and server side validation?
    Many people don't use both client-side and server-side, but then many people didn't believe that the earth revolves around the sun.

    You can guarantee that people without server-side validation are at immediate risk of security breaches.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,607
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Pullo View Post
    Do you know if many people bother to have both client side and server side validation?
    Anyone wanting to ensure that the entered info is valid will have server side validation.

    Anyone wanting to try to make things easier for their visitors will have client side validation.

    The only people who will not include both are those who have yet to learn what you have just learnt by asking the question in this thread.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,343
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Thanks for the replies.

    Quote Originally Posted by pmw57 View Post
    Got it in one, well done. That is the fundamental dichotomy of JavaScript. It's great to use, but you cannot rely on it actually being used.
    A dichotomy indeed. The more I read about it, the more I see what it can do, and the more it causes me headaches (by having to ensure that everything degrades gracefully).

    It's also annoying spending (not inconsiderable) time working on something that the client doesn't see or sometimes really appreciate (aforementioned graceful degradation).

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    After giving graceful degradation a go, web developers discovered more successful approach called progressive enhancement.

    You start with just the basic HTML/CSS and get things working that way. It's always possible to get things working, using forms and buttons.

    Then, you apply a layer of JavaScript over the top of that, where the script takes over the form submissions and does the work itself.

    Then you end up with a page that works in both scripted and non-scripted environments, and it's a lot easier to create by building forward in such a manner.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Do you know if many people bother to have both client side and server side validation?
    Everyone except rank amateurs, I should think.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •