SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery to make sure field is currency

    I have a text field with id=amount.

    How can I use JQuery to check that it only contains the digit 1234567890 and one and only one "." symbol?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if (/^\d+\.\d{2}$/.test($('#amount').val())) {
      // correct format
    }
    Note that the way I've set it up, it only allows two numbers after the dot. If you want it to be any number, replace the {2} with a +.

  3. #3
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested it with all the scenarios I can think of and it works perfect. Thanks.

  4. #4
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know in my specification I mentioned I wanted at least one decimal and then the code given is correct.

    However, in the event that if someone wanted the decimal portion to be optional then one can do ...

    Code:
    /^\d+(\.\d{2})?$/
    in case anyone else was interested.


Bookmarks

Posting Permissions

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