Validate decimal number in (known) national format

Posted by Álvaro G. Vicario on Stack Overflow See other posts from Stack Overflow or by Álvaro G. Vicario
Published on 2010-05-31T09:40:38Z Indexed on 2010/05/31 10:02 UTC
Read the original article Hit count: 179

Filed under:
|

PHP has many built-in functions to validate numbers but they all expect input in computer format: 1234.56. I need to validate stuff like 1,234.56 (English) or 1.234,56 (Spanish), together with some typical attributes (accepted range, max number of decimals, etc.).

I started writing my own function but I soon got lost. It's easy to read the user input into a float variable but I want to be able to detect numbers with invalid formats like 1,23456.7 or 1..25.

Do you have any recommendation on the subject?

(Decimal and thousand characters are known beforehand and it's okay to hard-code digit groups as three, there's no need to cover Indian number format.)

© Stack Overflow or respective owner

Related posts about php

Related posts about validation