regular expression for numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • baskarpr
    New Member
    • Oct 2006
    • 33

    #1

    regular expression for numbers

    I am new to regular expression. I have to validate an input, such as that should be non-negative numeric value (case 1) and non-negative numeric decimal value (case 2) (i.e. 0.5, 0.2, 10.0 etc) . I have tried out like below.

    [CODE=javascript]
    var reg2 = /^\d*\.{0,1}\d+$/;
    if(!reg2.test(s tr)) {
    alert("Not valid");
    }[/CODE]

    1. Is this enough to check for non negative numbers ?
    2. How can I check for non negative decimal values ?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    At the moment, your regular expression checks for:
    1. 0 or more digits (0-9)
    2. followed by 0 or 1 dots (there may not be a dot)
    3. followed by 1 or more digits
    So, it would match 0, 5, 0.2, .4, 5.5, etc.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, baskarpr.

      Checking for a non-negative is easy:
      [code=javascript]
      val = parseFloat(str) ;
      if(val < 0)
      {
      alert('Must not be negative.');
      }
      [/code]

      To check to make sure it has a decimal value, simply do this:
      [code=javascript]
      if(val == parseInt(val))
      {
      alert('Must have a decimal part.');
      }
      [/code]

      Comment

      • baskarpr
        New Member
        • Oct 2006
        • 33

        #4
        regular expression for numbers

        I am new to regular expression. I have to validate an input, such as that should be non-negative numeric value (case 1) and non-negative numeric decimal value (case 2) (i.e. 0.5, 0.2, 10.0 etc) . I have tried out like below.


        [code=javascript]
        var reg2 = /^\d*\.{0,1}\d+$/;
        if(!reg2.test(s tr)) {
        alert("Not valid");
        }[/code]


        1. Is this enough to check for non negative numbers ?
        2. How can I check for non negative decimal values ?
        Last edited by baskarpr; Aug 21 '07, 04:27 PM. Reason: changes

        Comment

        • baskarpr
          New Member
          • Oct 2006
          • 33

          #5
          Originally posted by pbmods
          Heya, baskarpr.

          Checking for a non-negative is easy:
          [code=javascript]
          val = parseFloat(str) ;
          if(val < 0)
          {
          alert('Must not be negative.');
          }
          [/code]

          To check to make sure it has a decimal value, simply do this:
          [code=javascript]
          if(val == parseInt(val))
          {
          alert('Must have a decimal part.');
          }
          [/code]

          I have tried with "< 0 " for negative numbers. But just I wanted to do that check in regular expression fo uniformity. Now I found it. Thanks to every one replied. :-)

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, baskarpr.

            Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Merged duplicate threads.

              Comment

              • baskarpr
                New Member
                • Oct 2006
                • 33

                #8
                Originally posted by pbmods
                Merged duplicate threads.
                hi, pbmods.. thanks for reply... Actually when i submitted and came back, I cant find my post. So I posted again. seems to be a bug... :-)

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, Baskarpr.

                  Originally posted by baskarpr
                  Actually when i submitted and came back, I cant find my post.
                  Click on the 'Control Panel' link in the top right of the page when you are logged in.

                  Comment

                  Working...