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.
Eduardo FuerteAsked:
Who is Participating?
 
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
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.