Solved

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

Posted on 2016-10-03
7
25 Views
Last Modified: 2016-10-09
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
Comment
Question by:Eduardo Fuerte
7 Comments
 
LVL 23

Expert Comment

by:Dr. Klahn
Comment Utility
Is the database table entry of type integer?
0
 

Author Comment

by:Eduardo Fuerte
Comment Utility
Hi

It is DOUBLE. But I guess comma is not acceptable in db as decimal separator... Is it configurable at MySQL?
0
 
LVL 20

Expert Comment

by:Russ Suter
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Accepted Solution

by:
Eduardo Fuerte earned 0 total points
Comment Utility
Is just a matter to use str_replace  changing "," by "." before to save, doing that the value is correctly saved at db table.
0
 

Author Comment

by:Eduardo Fuerte
Comment Utility
Hi @Russ

Our messages crossed. I'm going to check.
0
 
LVL 9

Expert Comment

by:Brian Tao
Comment Utility
You have to escape it with a "\" because dot (decimal point) is reserved in Regex. It matches a single character.
1
 

Author Closing Comment

by:Eduardo Fuerte
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now