javascript for valiadating textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cvijaykrishna
    New Member
    • May 2007
    • 22

    javascript for valiadating textbox

    hi,
    i have to validate a textbox on client side such that it should allow only float and integer values.
    pls suggest me in this regard and it would be helpfull if u give me a working example code.


    thanks and regards
    vijay
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    u can do during runtime .. not after giving the value in the text box.
    i mean .. while u r typing some keycode then u can validate.

    look at this sample code.

    <input type = text onkeypress = validate()>

    now the body of validate function is .... (IE specific)

    function validate()
    {
    var point_pressed = event.srcElemen t.value.indexOf ('.') ? true : false;
    if((event.keyCo de >=48 && event.keyCode <= 57) || (event.keyCode == 9 && !point_pressed) )
    //Check the point (.) keycode is 9 or anything else.
    {
    //allow the key_stroke .... (ie.....do nothing)
    }
    else event.keyCode = 0; //Block the key code.
    }
    i think u can arrange it.
    so try it. ... it ll work.

    kind regads.
    dmjpro.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by cvijaykrishna
      hi,
      i have to validate a textbox on client side such that it should allow only float and integer values.
      pls suggest me in this regard and it would be helpfull if u give me a working example code.


      thanks and regards
      vijay
      Consider using regular expressions. The regular expressions for float and integer can be found on this page for example.

      If you don't know regular expressions are, I suggest you read up about them.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by dmjpro
        u can do during runtime .. not after giving the value in the text box.
        i mean .. while u r typing some keycode then u can validate.

        look at this sample code.

        <input type = text onkeypress = validate()>

        now the body of validate function is .... (IE specific)



        i think u can arrange it.
        so try it. ... it ll work.

        kind regads.
        dmjpro.
        There are two problems with this:
        1. It only works in IE. That is not acceptable anymore.
        2. What if someone presses two dots or even more. That's not floating point.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Actually i mentioned eariler ... that it was IE specific.
          but these can be done by other browser supported JS.
          is not it?????
          because i m working in a project that is IE specific.
          so .. i m very much familiar with IE only.
          recently i m trying to learn cross-browser supported JS.

          and cvijaykrishna mentioned that he is looking for Floating Point validation that's why i did it.

          ACODER,
          sorry for againsting ur reply.

          kind regards.
          dmjpro.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by dmjpro
            and cvijaykrishna mentioned that he is looking for Floating Point validation that's why i did it.

            ACODER,
            sorry for againsting ur reply.
            No need to apologise. On looking at your code a second time, I can see that you make the check for the floating point, but a regular expression is much better for validation.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              yes u r right .... it is better to use API instead of wasting ur valueable time.

              kind regards.
              dmjpro.

              Comment

              • cvijaykrishna
                New Member
                • May 2007
                • 22

                #8
                hey
                tnx for those valuable suggestions i got it using regular expression validator.
                with regards and thanks
                vijay

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by cvijaykrishna
                  hey
                  tnx for those valuable suggestions i got it using regular expression validator.
                  with regards and thanks
                  vijay
                  Glad you got it working. Visit again!

                  Comment

                  • PerlPhi
                    New Member
                    • May 2007
                    • 5

                    #10
                    Originally posted by cvijaykrishna
                    hi,
                    i have to validate a textbox on client side such that it should allow only float and integer values.
                    pls suggest me in this regard and it would be helpfull if u give me a working example code.


                    thanks and regards
                    vijay
                    just use javascript regular expressions to lessen the overhead of posting back to the server.... :)

                    Comment

                    Working...