best form validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    best form validation

    Hii guys,
    My site has whole lots of forms...i wanted to know which is the best method for form validation.

    say using ajax or javascript and server side or any other..if there is any example it would be better
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    If I have to do form validation I usually run a client side validation (JS) so that the user may correct the entries and a server side validation to check if really all fields were filled correctly.

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      yeah

      I am also following the same thing as of now..i was thinking of Ajax ...but i got answers that
      it uses lots of server resourse ..

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        Originally posted by pradeepjain
        I am also following the same thing as of now..i was thinking of Ajax ...but i got answers that
        it uses lots of server resourse ..
        You don't need ajax for validation. If you've got a long form, I would assist the user along with JavaScript to get as much of the form right as possible. At that point I would use the bandwidth to validate. If my code is right and it's a legitimate user, I shouldn't get any failed validations server side, those people who are going to hack it, will get my server-side error message.

        If you've got a short forum and don't mind the traffic/resource of submitting the form for validation, I would just do it that way. Why do the wort twice.

        Second reason to do it on client-side too is for usability. It's so nice to have a phone number formatted and counted to make sure I didn't miss a digit. Some forms put a green check mark next to each field I fill out if its correct and a red X for those that are incorrect after I've left that field.


        Dan

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          yup the thing u told putting x mark and tick mark needs ajax scripting i guess

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            Originally posted by pradeepjain
            yup the thing u told putting x mark and tick mark needs ajax scripting i guess
            no not really. Depends on what your validating. If your validating an email address, you can use JavaScript since you already know what an email "should" look like.

            But for example you need to check if a username has been taken, you need to query your database, and for this you need Ajax.




            Dan

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #7
              my form does need to validate only few fields like phone,email..et c....i use pop up msg on submit ......can u show me an example for wht u told like x mark and tick mark with javascripts....

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                it is basic JavaScript really, there's no trick. All your doing is changing the display value of an image. (See Google for more info) You have two images that are off by default. (Display: none with CSS). With javascript you turn on the one that corresponds to the validation.

                simple example:
                Code:
                if(valid)
                {
                  document.getElementById('emailImageCheck').style.display = "block"; // or inline 
                }  
                else
                {
                  document.getElementById('emailImageRedX').style.display = "block"; // or 
                }



                Dan

                Comment

                • pradeepjain
                  Contributor
                  • Jul 2007
                  • 563

                  #9
                  i guess u were talking of form like this
                  Javascript form validation - doing it right

                  i really dont understand without any examples :(

                  Comment

                  • dlite922
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1586

                    #10
                    Originally posted by pradeepjain
                    i guess u were talking of form like this
                    Javascript form validation - doing it right

                    i really don't understand without any examples :(
                    Yes, like that. except instead of red text you would do the lines I typed in my previous post.

                    You learn coding by trying. Beginning coders copy and paste. Then you change the code. And then you learn to memorize and use references and Google less and less.

                    This is a path everybody takes. No one learns outright code by memorization.


                    Dan

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      You need a combination of both Javascript, although it isn't required, and PHP. If you're going to use one and not the other, then you must use PHP. The reason to this is: Javascript is not always enabled on the browser, and if your validation relies on javascript, in a browser that doesn't have it enabled, your validation is useless.

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Moving to the Javascript forum for now.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          pradeepjain,

                          post your code and explain what you've tried and the parts you're struggling with. In the link, you can just view the source to see the code and adapt it to your requirements.

                          Comment

                          Working...