validate input field for numeric value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idsanjeev
    New Member
    • Oct 2007
    • 241

    validate input field for numeric value

    I try to validate inpute field for weight that accept only numeric value like 65.12 not allow any special character or character but allow '.' .
    thanks
    jha
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    What have you attempted so far?

    Comment

    • idsanjeev
      New Member
      • Oct 2007
      • 241

      #3
      Originally posted by acoder
      What have you attempted so far?
      i am using this code for validation but this way only can check null,(-)value,and character with special caracter. but don't know how can do with my requirement like 45.10

      Code:
      function ck_wt(obj)
      {
      if(obj.value=="")
      {
      obj.focus();
      alert("Wt. Must be entered.")
      }
      else if(obj.value<0 || obj.value=="")
      {
      obj.focus();
      alert("wait must be +ve")
      return;
      }
        else if(!obj.value.match(/^\d+$/)){
          obj.focus();
      	alert("Only numbers are allowed.");
      	return;
        }
      }
      thanks
      jha

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Either use parseFloat and isNaN or use a simple float regular expression, e.g. /^\d{2}\.\d{2}$/

        Comment

        • idsanjeev
          New Member
          • Oct 2007
          • 241

          #5
          Originally posted by acoder
          Either use parseFloat and isNaN or use a simple float regular expression, e.g. /^\d{2}\.\d{2}$/
          the code for check wait is modified by but not work it accepted special character like 4.5a or 4.5;aq but i wants to 4.5 only or 4

          Code:
          function ck_wt(obj)
          {
          if(obj.value=="")
          {
          obj.focus();
          alert("Wt. Must be entered.")
          }
          else if(obj.value<0 || obj.value=="")
          {
          obj.focus();
          alert("wait must be +ve")
          }
          else if(obj.value.match(/^\d{2}\.\d{2}$/))
          {
          obj.focus();
          alert ("testing")
          return;
          }
          }
          jha

          Comment

          • maminx
            New Member
            • Jul 2008
            • 77

            #6
            Originally posted by idsanjeev
            the code for check wait is modified by but not work it accepted special character like 4.5a or 4.5;aq but i wants to 4.5 only or 4

            Code:
            function ck_wt(obj)
            {
            if(obj.value=="")
            {
            obj.focus();
            alert("Wt. Must be entered.")
            }
            else if(obj.value<0 || obj.value=="")
            {
            obj.focus();
            alert("wait must be +ve")
            }
            else if(obj.value.match(/^\d{2}\.\d{2}$/))
            {
            obj.focus();
            alert ("testing")
            return;
            }
            }
            jha
            i'm using this library below, u can use this

            [CODE=javascript]function numbersonly(myf ield, e, dec) {
            var key;
            var keychar;

            if (window.event)
            key = window.event.ke yCode;
            else if (e)
            key = e.which;
            else
            return true;
            keychar = String.fromChar Code(key);

            // control keys
            if ((key==null) || (key==0) || (key==8) ||
            (key==9) || (key==13) || (key==27) )
            return true;

            // numbers
            else if ((("-0123456789").in dexOf(keychar) > -1))
            //else if ((("0123456789" )))
            return true;

            // decimal point jump
            else if (dec && (keychar == "."))
            {
            myfield.form.el ements[dec].focus();
            return false;
            }
            else
            return false;
            }[/CODE]


            and to call that function in HTML form is with this event below :

            onkeypress="ret urn numbersonly(thi s, event,true)"


            hope usefull,

            kind regards, maminx
            Last edited by gits; Aug 1 '08, 08:50 AM. Reason: remember to use CODE tags !!!!

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              Originally posted by maminx
              i'm using this library below, u can use this

              [CODE=javascript]function numbersonly(myf ield, e, dec) {
              var key;
              var keychar;

              if (window.event)
              key = window.event.ke yCode;
              else if (e)
              key = e.which;
              else
              return true;
              keychar = String.fromChar Code(key);

              // control keys
              if ((key==null) || (key==0) || (key==8) ||
              (key==9) || (key==13) || (key==27) )
              return true;

              // numbers
              else if ((("-0123456789").in dexOf(keychar) > -1))
              //else if ((("0123456789" )))
              return true;

              // decimal point jump
              else if (dec && (keychar == "."))
              {
              myfield.form.el ements[dec].focus();
              return false;
              }
              else
              return false;
              }[/CODE]


              and to call that function in HTML form is with this event below :

              onkeypress="ret urn numbersonly(thi s, event,true)"


              hope usefull,

              kind regards, maminx
              @maminx: according to the posting guidelines USE THE CODE TAGS ... i'm getting tyred fixing every single post by you with that ...

              regards,
              MOD

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by idsanjeev
                the code for check wait is modified by but not work it accepted special character like 4.5a or 4.5;aq but i wants to 4.5 only or 4
                Oh, I see you want any type of float number. What I suggested earlier was a very simple version from what you posted, but it seems you need something a little more complete. This link should help.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by maminx
                  i'm using this library below, u can use this

                  ...
                  It allows multiple "." decimal characters.

                  Comment

                  Working...