regex for currency (euro)

前端 未结 3 1654
鱼传尺愫
鱼传尺愫 2020-12-20 07:20

i am trying this code for make a validation for a value. (regex from this site)

UPDATE:

Now i have

$value1=250;    

$value2=10000;

                 


        
相关标签:
3条回答
  • 2020-12-20 07:44

    This code below solved my problem:

    if (!preg_match("/^(([^0]{1})([0-9])*|(0{1}))(\,\d{2}){0,1}€?$/", $form['salary'])) {
        echo "invalid";
        return false;
    } else {
        $value1 = 400;
        $value2 = 10000;
        $salary = $form['salary'];
        $salary = preg_replace('/[€]/i', '', $salary);
        if($salary < $value1 || $salary > $value2) {
            echo "bad values";
            return false;
        } else {
            echo "valid";
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-12-20 07:52

    My contribution. It works great.

    final Pattern pattern = Pattern.compile("^([0-9]+)|((([1-9][0-9]*)|([0-9]))([.,])[0-9]{1,2})$");
    
    0 讨论(0)
  • 2020-12-20 08:05

    The regex solution would look like this

    ^(?:10000|(?:(?:(?:2[5-9]\d)|[3-9]\d{2}|\d{4})(?:[,.]\d{2})?))€?$
    

    See here online on Regexr

    But it would be better for checking if a value belongs to a range, not to use a regex. You can extract the value easily and do a normal <> check on numbers outside.

    0 讨论(0)
提交回复
热议问题