Validating form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mainul
    New Member
    • Sep 2006
    • 51

    Validating form

    Hi
    i dont have much idea in javascript.i have a web application where i would like to validate a text field. user will only be able to insert floating number (like 4.5, 10.45). no letter will be allowed.

    please assit.

    Best regards
    Mainul
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    try thsi code in js
    function validate()
    {
    // to access text value do ======= document.all.te xtfield_name.va lue
    }
    plz send me what u want to do
    i am online

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      If you know regular expressions, use the following for floating point number:
      Code:
      /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
      then test:
      Code:
      var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
      if (!reFloat.test(yourformfield)) alert("Error: not a floating point number")

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by dmjpro
        try thsi code in js
        function validate()
        {
        // to access text value do ======= document.all.te xtfield_name.va lue
        }
        plz send me what u want to do
        i am online
        document.all is old code and IE-specific. You should use document.getEle mentById("textf ieldid").value instead.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          sorrryyyyyyyyyy yyyyy
          without reading the problem i responded...
          u can stop user to press alphabatic word...
          try this code ....
          <input type = text onkeypress = test()>

          function test()
          {
          if(!(event.keyC ode>=49&&event. keyCode<=57)) event.keyCode = 0;
          if(event.keyCod e == point_keycode && event.srcElemen t.value.indexOf ('.') == -1) event.keyCode = 0;
          }

          to get the point_keycode try this code ...
          <body onkeypress = test()>
          function test()
          {
          alert(event.key Code);
          }

          plz press any key u want to know it's keyCode..
          plz maintain the case sensivity while u writing the code..
          any problem while u write the code plz send me ur problem.....
          i am online

          Comment

          • mainul
            New Member
            • Sep 2006
            • 51

            #6
            thanks a lot guys for quick response. let me try the codes.

            best regards
            Mainul

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              ok try it quickly and send me ur result
              i am eager to know the result

              Comment

              • karthi84
                Contributor
                • Dec 2006
                • 270

                #8
                Originally posted by dmjpro
                sorrryyyyyyyyyy yyyyy
                without reading the problem i responded...
                u can stop user to press alphabatic word...
                try this code ....
                <input type = text onkeypress = test()>

                function test()
                {
                if(!(event.keyC ode>=49&&event. keyCode<=57)) event.keyCode = 0;
                if(event.keyCod e == point_keycode && event.srcElemen t.value.indexOf ('.') == -1) event.keyCode = 0;
                }

                to get the point_keycode try this code ...
                <body onkeypress = test()>
                function test()
                {
                alert(event.key Code);
                }

                plz press any key u want to know it's keyCode..
                plz maintain the case sensivity while u writing the code..
                any problem while u write the code plz send me ur problem.....
                i am online
                hi dmjpro,
                the coding is good. can u get me the key code for special keys like *,-,+ etc.

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  can't u get it from my code.....

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    To get the event code across all browsers and not just IE, use
                    Code:
                    function setEvent(Id) {
                     Id.onkeypress = function keyPress(evt) { 
                      if (evt) 
                       keyCode = evt.keyCode ;
                      else  
                       keyCode = event.keyCode ;
                     alert(keyCode);
                     }
                    }

                    Comment

                    Working...