0

I'm a bit stuck here.
With regards to the input of accounting data, the analyst requested a specific set of rules on the input of decimal data in text boxes.
Since I have not studied regular expressions at this point, and because I have very strict deadlines to attend with each a lot of work, I request your help.

The rules are (on blur):

  • IE8+ compatible;
  • Set default at 2 digits behind the comma;
  • Disallow other characters than ".", "," and numeric characters;
  • If there are multiple separators in the returned number, only keep the last one.
    Explanation:
    Employees may have different regional settings, and comma / dot may be used as either decimal or thousands separator.
    In case an employee copy - pastes both the thousand and the decimal separator, it has to be ignored.

What I've done so far, but doesn't fulfill the requirements:
http://jsfiddle.net/NxFHL/1/

$('#test_rules.numeric').on('blur', function(){
    var myString = $('#test_rules.numeric').val(); 
    myString = parseFloat(myString.replace(/[^\d.-]/g, '')); 
    myString = toFixed(myString, 2); 
    console.log(myString); 
}); 

function toFixed(value, precision) {
    var power = Math.pow(10, precision || 0);
    return 

String(Math.round(value * power) / power);
}

The regular expression used doesn't work correctly as it only accepts dot, not comma.
Also I am not sure about how I should make it so that only the last separator stays (so that the thousands separator gets ignored).

4
  • 1
    To "ignore" the previous seperators you will probably need to find what is last (i.e. , or .) and then replace the other with a blank string. Commented Feb 17, 2014 at 11:13
  • You don't need to parse input value as float. You only have to apply some regex to myString Commented Feb 17, 2014 at 11:17
  • Thanks for the tips. As for the regular expression for the replacement of separators except for the last one: I will study regex, but currently I am having a pile of other requirements to deal with and I'm working against a deadline for next Friday. Thanks for your suggestions on that matter. Commented Feb 17, 2014 at 11:20
  • @hindmost Unless the correct numeric value needs to be used later on in the script. Commented Feb 17, 2014 at 11:21

2 Answers 2

1

Try this function:

function parseInputNum(val) {
    var sep = val.lastIndexOf('.') > val.lastIndexOf(',')? '.' : ',';
    var arr = val.replace(new RegExp('[^\\d'+sep+']+', 'g'), '')
    .match(new RegExp('(\\d+(?:['+sep+']\\d+|))$'));
    return arr? arr[1].replace(/[,]/g, '.') : false;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Kim Gysen Sorry. There was syntax mistake. Fixed
@Kim Gysen Updated. Although now it doesnt round it by 2 digits. You have to do it yourself
0

You can use this pattern:

^(?:[1-9](?:[0-9]{0,2}(?:([.,])[0-9]{3})?(?:\1[0-9]{3})*|[0-9]*)|0)(?!\1)[.,][0-9]{2}$

This pattern will check numbers like:

123,45
12,45
0.45
123,456,789.12
1234.56
123.456.789,12

but not numbers like:

12.23.45,12
012,345,678,91
1234,567.89
123,456,78

To convert the string into a number you must remove the thousand delimiter before. This can easily be done since the delimiter (if present) is in the capturing group 1. You must probably too replace the , by the . if it is used as decimal separator.

3 Comments

Uhm, I'm being confused sorry. You mention that it works for the first series of numbers but not the second? Not sure if I'm getting this right; the problem at our department is that there are people from different nationalities (Russian, Chinese, Spanish) and their regional settings differ from person to person. So how do I know if I can implement this reg ex reliably?
@KimGysen: If you want to have a more precise idea about number formats, you can take a look at this page: codeproject.com/Articles/78175/International-Number-Formats You can see that you can easily adapt the pattern (by adding the space in the character classes), to support russian format. Chinese and Spanish seems to have the same format.
Thanks for your input. I will read this as soon as I get the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.