Results 1 to 6 of 6

Thread: Javascript validation in Forms??

  1. #1
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript validation in Forms??

    Hi

    I've desgined the form that I wish to use on my website but am having problems with the validation. I can get one or the other of the two functions working just not both. I want an error message when the user does'nt fill in the fields correctly and a 'thank you for summiting' pop up if the form is filled out correctly.

    How can I get both of them to work at wonce?

    <script>

    /*
    Check required form elements script-
    By JavaScript Kit (http://javascriptkit.com)
    Over 200+ free scripts here!
    */

    function checkrequired(which){
    var pass=true
    if (document.images){
    for (i=0;i<which.length;i++){
    var tempobj=which.elements[i]
    if (tempobj.name.substring(0,8)=="required"){
    if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1)){
    pass=false
    break
    }
    }
    }
    }
    if (!pass){
    alert("Error Message here")
    return false
    }
    else
    return true
    }

    </script>
    Script for error message

    <script type="text/javascript">
    function createTarget(t){
    window.open("comfirm", t, "width=300,height=200");
    return true;
    }
    </script>
    Script for pop-up.



    Thanks in advance
    Last edited by soupdragon; 09-06-2006 at 01:33 PM.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    JavaScript is a bad way to do this, your users just have to disable JavaScript, and then the validation is completely gone.
    - Mike

  3. #3
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the help. Would it then be best to code all or some of it in php. Could I create the validation in Javascript and the popup in php? This however would not stop the user disabling the javacript?

    What would you recommend to be the best wy to do this?

    Thanks again

  4. #4
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Still can't get it to work!! Does anybody know of or have script that they could direct me to that would allow me to validate the fields and display a conformation?

    Also which lanuage is best to code this in? i have no itea where to start with php so at the moment Javascript is the only option that I have I think?

  5. #5
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Problem solved!! I'd forgotten the && between events!!

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You should still do this pretty much entirely server-side. Javascript is useful here only to prevent the user having to wait to submit the form and get a response before realising that s/he has done something wrong; it shouldn't be relied upon.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •