SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Some help needed for currency validation

    I need to include some validation for currency (specifically for two decimal places) and I believe that I need to use some form of regular expression.

    I've seen scripts that include a long string that contains all the characters that you do not want to allow, but I have never really understood how they work.

    I'd really appreciate it if someonw could show me a simple validation script and offer some explanation as to how it's working. I may take one look at it and go 'aha, I know what's going on there', but then again, I may not ;-)

    Thanks once again - hope you can help ... whoever 'you' are
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  2. #2
    SitePoint Zealot Arkkimaagi's Avatar
    Join Date
    Mar 2002
    Location
    Lappeenranta, Finland
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What kind of validation are you looking for? What do you want to accomplish? (some sample data could not hurt)

  3. #3
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quite simple. I want to reject anything that

    * isn't numeric
    * contains any symbols (apart from '.')
    * has above two decimal points (so, 2.00 is ok, but 2.001 not ok)

    In short, if it looks like a UK currency format, I'd like that to validate, anything else throw it out.
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    !varaible.match(\d+\(.\d+\)?)

    That'd make sure it matches the format of a number - followed optionally by a . and more numbers...

    is this the kind of thing that you wanted?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I posted while you were posting. i think i covered everything except the 2 fixed points - if you wanted to have that then you'd have {2} instead of the + inside the brackets.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  6. #6
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Flawless_koder
    !varaible.match(\d+\(.\d+\)?)

    That'd make sure it matches the format of a number - followed optionally by a . and more numbers...

    is this the kind of thing that you wanted?

    Flawless
    If it works, it's what I'm after, thanks very much.

    But what I'm really after is a short explanation as to how this works. I don't understand regular expressions, simply cos I've never used them.

    Any chance you could spell this out for me? For example, what is d in this instance? Is it a variable name? I guess I need it dissecting, as it looks like just a bunch of symbols/shorthand.

    Thanks so far, guys
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  7. #7
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *** Correction : \. ***
    !varaible.match(\d+\(\.\d+\)?)
    Okie:

    ! - If this is NOT true then do your action

    \d - a number
    + - One or more of the previous character
    () - a group of characters - used if you want to
    \. - a . ( . is any character )
    ? - the previous character or group is optional, if it appears it should appear in the format required.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  8. #8
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Monsieur Flawless, I will try bunging this in later ...
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  9. #9
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    s'ok, let me know if it doesn't work

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •