Validating the SQL Query entered by user using Java Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuradha135
    New Member
    • Jan 2008
    • 11

    Validating the SQL Query entered by user using Java Script

    I have a textarea in my jsp, where in the user will enter a sql query of select or insert or update or delete.I have to validate that entered data and say whether the query is syntatically correct or not.For Example if User enters,
    Select * from Tab..:
    like this, it has to throw an alert message saying that, sql query is invalid as the user entered ..: at the end of the query.

    I have to do this in javascript.
    Can any one help me out?

    Thanks&Regards
    Anuradha
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Since you have to do this in JavaScript, it is not a Java problem, now is it? On the other hand, what if the user turns off scripting in their browser?

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      This is not a trivial task! You will have to parse the input and check for mis-spellings, syntax errors and lots more. Perhaps you should make a few basic checks instead.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by BigDaddyLH
        what if the user turns off scripting in their browser?
        You can make checks on the client-side as a backup (and it saves a trip to the server just to be met with an error message). The real checking should always be made on the server-side, of course.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by acoder
          You can make checks on the client-side as a backup (and it saves a trip to the server just to be met with an error message). The real checking should always be made on the server-side, of course.
          Indeed. Me, I always turn off JavaScript. Speeds up the browser ;-)

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by BigDaddyLH
            Indeed. Me, I always turn off JavaScript. Speeds up the browser ;-)
            You don't know what you're missing! ;)

            JavaScript does have its uses, just that sometimes it's misused. It's not like what it used to be, though.

            Comment

            • anuradha135
              New Member
              • Jan 2008
              • 11

              #7
              Can you please let me know the example or send a sample code?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                For validation, you can use regular expressions. Post your code so far.

                Comment

                • anuradha135
                  New Member
                  • Jan 2008
                  • 11

                  #9
                  till now i didn't started any coding on this.i have yet to start.Thought i will get some idea to proceed and theni can start

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    The first step is to add an onsubmit to the form:
                    [html]<form ... onsubmit="retur n validate(this); ">[/html]

                    Then your validation function would return true if it passes validation and false otherwise.

                    What exactly are you checking?

                    Comment

                    • anuradha135
                      New Member
                      • Jan 2008
                      • 11

                      #11
                      Originally posted by acoder
                      The first step is to add an onsubmit to the form:
                      [html]<form ... onsubmit="retur n validate(this); ">[/html]

                      Then your validation function would return true if it passes validation and false otherwise.

                      What exactly are you checking?

                      i got that functonality.. thank you.....

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by anuradha135
                        i got that functonality.. thank you.....
                        Glad to hear that you got it working. Can you post your solution here for the benefit of others.

                        PS. I split your other posts into a new thread.

                        Comment

                        • anuradha135
                          New Member
                          • Jan 2008
                          • 11

                          #13
                          Originally posted by acoder
                          Glad to hear that you got it working. Can you post your solution here for the benefit of others.

                          PS. I split your other posts into a new thread.

                          i did it thrrought backend..means, once the user enter the sql in text box, am executing the query in DAO and if the query returns any records or not with out error, then it is a valid query.Else am throwing the exception that query is not valid.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by anuradha135
                            i did it thrrought backend..means, once the user enter the sql in text box, am executing the query in DAO and if the query returns any records or not with out error, then it is a valid query.Else am throwing the exception that query is not valid.
                            Thanks for that. Yes, that would be the preferred way in this case (though you could make an XMLHttp [Ajax] request using JavaScript.

                            Comment

                            Working...