Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 55
  • Last Modified:

Could you point what must be changed in this regex to accept two decimal places?

Hi experts

Could you point what must be changed in this regex to accept two decimal places?
regex:/^[0-9]{1,2}([,.][0-9]{1,2})?$/

Open in new window


When using this it accepts correctly only digits with ","   and "."  but when saved to DB table the decimal part is truncated.

Thanks in advacne.
0
Eduardo Fuerte
Asked:
Eduardo Fuerte
1 Solution
 
Dr. KlahnPrincipal Software EngineerCommented:
Is the database table entry of type integer?
0
 
Eduardo FuerteAuthor Commented:
Hi

It is DOUBLE. But I guess comma is not acceptable in db as decimal separator... Is it configurable at MySQL?
0
 
Russ SuterCommented:
Not sure what your code is doing that's processing the data but it could have to do with the fact that the decimal part is in a numbered capture group and the whole part is not. Try this:?

^([0-9]{1,2}(?:[,.][0-9]{1,2}))?$

This puts the entire expression into a single numbered capture group. If that doesn't work you can try putting the entire expression outside a capture group like this:

^[0-9]{1,2}(?:[,.][0-9]{1,2})?$
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Eduardo FuerteAuthor Commented:
Is just a matter to use str_replace  changing "," by "." before to save, doing that the value is correctly saved at db table.
0
 
Eduardo FuerteAuthor Commented:
Hi @Russ

Our messages crossed. I'm going to check.
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
You have to escape it with a "\" because dot (decimal point) is reserved in Regex. It matches a single character.
1
 
Eduardo FuerteAuthor Commented:
Hi

The regex I used since the start is OK.
It's a matter of use the correct format  to insert values in MySQL.

Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now