Results 1 to 7 of 7
  1. #1
    Join Date
    May 2008
    Location
    USA
    Posts
    114

    javascript regular expression (integers except 0)

    Hi,

    I want to write a validation function that will validate a field in javascript, the field should contains integer numbers only except 0, I wrote the following but it accepts 0, how can I exclude 0 from it?

    here is my function:

    PHP Code:
    function checkQty(qty
    {
    var 
    illegalChars= /[^\d]/
    if (
    qty.value.match(illegalChars)) {
    alert("Integer number only accepted");
    qty.select();
    qty.focus();
    return 
    false;
    }
    else
    {
    return 
    true;
    }

    Thanks

  2. #2
    You have two choices, there are probably others as well.

    1. if it passes the current test, use Math.Floor to find out if it is at least 1, or simply "if((qty.value-0)>0) alert('this is ok');"

    2. if it passes the current test, use a second regexp to test for the digit 0

    #1 is safer, because what if the quantity is 10?, or they input 03?
    edgedirector.com
    managed dns global failover and load balance (gslb)
    exactstate.com
    uptime report for webhostingtalk.com

  3. #3
    Join Date
    May 2008
    Location
    USA
    Posts
    114
    you are right, Thanks you

    Also, I should exclude minus numbers.

    here is the latest code and it works:

    PHP Code:
    function checkQty(qty
    {
    var 
    illegalChars= /[^\d]/
    if (
    qty.value.match(illegalChars)) {
    alert("Integer number only accepted");
    qty.select();
    qty.focus();
    return 
    false;
    }
    else if(
    qty.value <= 0)
    {
    alert("quantity should be greater than 0");
    qty.select();
    qty.focus();
    return 
    false;
    }
    else
    {
    return 
    true;
    }


  4. #4
    Join Date
    May 2007
    Location
    Orange Country, CA
    Posts
    138
    Try something like this (I haven't tested it, but it's logically correct):

    PHP Code:
    function checkQty(qty) {
         var 
    legal_chars = /[1-9]+/

         if (
    qty.value.match(legal_chars))
              return 
    true;
         
    alert("Integer number only accepted");
         
    qty.select();
         
    qty.focus();
         return 
    false;

    Basically the above says, if qty.value consists of characters 1 through 9 one or more times consecutively we have a match so return true, otherwise return false.

    EDIT:

    Also, if you want to allow leading zeros use this which reads, "zero or more 0's followed by one or more characters consisting of 1 through 9":
    PHP Code:
         var legal_chars = /0*[1-9]+/ 
    Last edited by Codelphious; 09-13-2009 at 12:17 PM.
    Blesta - Professional Billing Software
    We are about creating good experiences
    Trial - Demo | 866.478.7567 | Twitter @blesta

  5. #5
    Join Date
    May 2009
    Posts
    766
    This is not logically correct...it will fail on the number 10.

  6. #6
    Join Date
    May 2008
    Location
    USA
    Posts
    114
    Hi,

    It is working with me, and it accepts 10, but it accepts minus too.

    I prefer to use the code with if statement to check the qty if <= 0 to use different message that when character inserted instead of digit

  7. #7
    why not just

    if(whatever > 0 && !isNaN(whatever)) {
    dosomething()
    }

    Edit: if you wanted a function it'd be:

    function checkQty(num) {
    return (whatever > 0 && !isNaN(whatever)) ? true : false;
    }
    Last edited by Cmafai; 09-14-2009 at 08:19 AM.

Similar Threads

  1. JavaScript Regular Expression
    By latheesan in forum Programming Discussion
    Replies: 8
    Last Post: 07-30-2009, 02:46 PM
  2. Regular Expression Help
    By smkied in forum Programming Discussion
    Replies: 1
    Last Post: 11-21-2008, 02:37 PM
  3. Using Regular Expression to...
    By NetVer in forum Programming Discussion
    Replies: 5
    Last Post: 12-30-2004, 08:16 AM
  4. Is my regular expression right?
    By eagleknight in forum Web Design and Content
    Replies: 9
    Last Post: 10-14-2004, 12:36 AM
  5. PHP regular expression
    By apatit_ru in forum Programming Discussion
    Replies: 9
    Last Post: 11-20-2003, 08:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •