How to Translate this Code from VBScript to JavaScript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicebasic
    New Member
    • Aug 2010
    • 91

    #16
    It works with no problem in IE.

    The problem is with FireFox. I changed the codes from VBScript to JavaScript using the help of Experts in this forum.

    It seems something is wrong with quotations or something like that.

    If you leave the boxes empty and press Submit, no Alerts will be displayed.

    If you fill up the boxes and press Submit, nothing will happen.

    I'm really confused.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #17
      Like I said in my previous post, it's going to run as soon as the page loads and before the form elements are available for use. You need to tie it to the event on the button

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #18
        Rabbit is right!
        That JavaScript code is not within a function so it is going to be executed when the page loads. Put it into a function and call the function during the button click event.

        -Frinny

        Comment

        • nicebasic
          New Member
          • Aug 2010
          • 91

          #19
          The original code that used VBScript was like this:

          Code:
          <SCRIPT LANGUAGE=vbscript Event=OnClick For=btnConf>
          .......
          </SCRIPT>
          Can we change the code to this?

          Code:
          <SCRIPT LANGUAGE=JavaScript Event=OnClick For=btnConf>
          .......
          </SCRIPT>
          Is this a valid change?

          Comment

          • nicebasic
            New Member
            • Aug 2010
            • 91

            #20
            Fortunately, using your great advice, I added a new JavaScript Function like this:
            Code:
            <script language="JavaScript">
            function cancel(){
            window.location = './cancel.asp'
            }
            </script>
            When you click on the Cancel Button, the above JavaScript will redirect you to the Cancel page and the variables of the current page will be lost.

            But:

            I have made some changes to the code as you mentioned. Your instructions were really great. I put the code in a JavaScript function like this:

            Code:
            <script type='text/javascript'>
            function decide(){
            ..........
            }
            </script>

            but I get this error message in FireFox Error Console:


            invalid assignment left-hand side
            Code:
            else if(txtPhone.value = '' || isNaN(txtPhone.value) = false)
            Last edited by nicebasic; Apr 10 '12, 06:41 PM.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #21
              If you're trying to compare values, you use ==. If you're trying to assign a value, you use =.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #22
                Ooops, sorry that's my fault. I remember translating the vbScript into JavaScript quickly and not testing anything. I edited/fixed my original post so that it uses the proper comparison operators in the if statements instead of trying to assign things.

                -Frinny

                Comment

                • nicebasic
                  New Member
                  • Aug 2010
                  • 91

                  #23
                  I edited this line from:
                  Code:
                  else if(txtPhone.value = '' || isNaN(txtPhone.value) = false)
                  to
                  Code:
                  else if(txtPhone.value = '')
                  and the above error disappeared.

                  But another error shows up:

                  document.getElm entById is not a function

                  Can't we replace document.getElm entById by something else?

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #24
                    You should have changed the single "=" to a double "==" in the if statements...

                    Like this:
                    Code:
                    else if(txtPhone.value == '' || isNaN(txtPhone.value) == false)

                    -Frinny

                    Comment

                    • nicebasic
                      New Member
                      • Aug 2010
                      • 91

                      #25
                      Thank you, Frinavale and Rabbit and jhardman.

                      I found a problem in this line:
                      Code:
                      var txtPhone = document.getElmentById('txtPhone');
                      This very simple problem prevented the code from running. I changed it to:
                      Code:
                      var txtPhone = document.getElementById('txtPhone');

                      It was a misspelling. Changing "getElmentB yId" to "getElementById " fixed it all.

                      I'm checking the other parts now.

                      I'm really grateful to Frinavale and Rabbit for your really useful support.

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #26
                        Did you put in those ==? Because you may not get an error but the results would be wrong if you didn't.

                        Comment

                        • nicebasic
                          New Member
                          • Aug 2010
                          • 91

                          #27
                          Yes, Rabbit. I used your great help and great support. Without your help, it wouldn't be possible to solve this complicated problem.

                          Since this line causes a problem:
                          Code:
                          else if(txtPhone.value == '' || isNaN(txtPhone.value) == false)
                          I removed the second part of the condition and changed it to:
                          Code:
                          else if(txtPhone.value == '')
                          I don't know why it doesn't accept any numbers. I enter a number, but it gives me an Alert that I have to enter a valid number. I didn't know how to fix it, so I removed it.

                          Thank you again, Rabbit.

                          You contributed so much. You referred to a key point here:

                          it's going to run as soon as the page loads and before the form elements are available for use. You need to tie it to the event on the button

                          Comment

                          Working...