SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie Javascript questions

    Hello all!
    I am starting to use JS a couple of days now and mainly I am interested in using it in form validation and to create pop-ups.
    I have 2 questions :

    1) If a user doesn't have JS enabled, how will I check for empty fields in the form and show an alert box that prompts for corrections?

    2) Also, I have seen sites that have the following : There is a link somewhere which works like this : [a] if you have JS enabled, it "recognizes" it and gives a pop-up window when clicked whereas [b] if you don't have JS enabled, it simply acts like a link and opens a new window (not a poup-up one).

    Because I am a bit confused, I am staring to think that maybe JS causes more problems than it solves... Should a programmer use it or ,for instance, do a form validation in another page and don't count on the user having JS enabled?

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) You can't. You can validate a form on the server-side and return invalid ones back to the form page with errors marked, but an alert is purely Javascript; no JS, no alert.

    2) This is very easy to achieve:
    Code:
    <a href="dest.html" onclick="popup(this.href); return false;">Click me</a>
    (where the popup() function includes the window.open code). If JS is available the onclick event will be fired, and return false stops the actual link being followed.
    Quote Originally Posted by ktsirig
    ...don't count on the user having JS enabled?
    Absolutely - never assume (unless you're designing for a closed environment like an intranet).

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should a programmer use it or ,for instance, do a form validation in another page and don't count on the user having JS enabled?
    Think of it this way. It the user has js enabled, which is a high percentage of users, you will save a hit to your server when js intercepts the form and alerts the user about mistakes. You should still do validation checks on the server side, as well, for everything that is submitted.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember you can use vbscript as well... Not highly supported but usable?

    Gav


  5. #5
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In addition to the possibility of JavaScript being disabled, server-side validation is important to security. Even with JS enabled, a malicious user could fashion SQL injection, buffer overruns, or other attacks. Always validate any input coming to the server. As the others have pointed out, client-side validation is little more than a convenience. It doesn't give you any real control over what is being sent.


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
  •