convert decimal to string, internationally (points and commas)

Hello all,

on a webpage I need to compare the value of an input field to a number value in script. I am sure that a number was entered (I wrote a long RegExp).

Two problems:

1) the site is international, so users in the USA may enter 123,456,789.123, while the same number is entered in Europe as 123.456.789,123. Furthermore, users are not forced to enter the digit grouping... They might as well just enter 123456789.123 or 123456789,123... How can I be sure that I get the correct value back?

2) I can't figure out how to do the conversion. If I use parseFloat, I always get back the first grouping of numbers, which is always smaller than 1000.

The code:

var strRE = /^\d{0,3},(\d{3},)*\d{3}(\.*\d)?$|^\d*(\.*\d)?$|^\d{0,3}\.(\d{3}\.)*\d{3}(,*\d)?$|^\d*(,*\d)?$/;
var re = new RegExp(strRE);

if (document.formname.fieldname.value.match(re)) {
    alert("Successful match");
   
    if (document.formname.fieldname.value.parseFloat>1000) {
        alert("bigger");
    } else {
        alert("smaller");
    }
}

Hope you can help me out !

SpSp
OzzymandiasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

knightEknightCommented:
force them to enter only numeric characters and .

<INPUT type="text" name="numeric" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d\.]*/gi,"");' />
0
knightEknightCommented:
... with a note about using . as decimal (instead of ,)
0
knightEknightCommented:
FYI also, your IF statement is malformed ...

if (1*document.formname.fieldname.value>1000) {
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

RichieHindleCommented:
What you're asing is impossible.  If the user enters "123,456" you don't know whether that comma is meant as a thousand separator or a decimal separator.

You need to either do as knightEknight suggests and limit the kind of input, or add another control (say, a pair of radio buttons) where the user selects his preferred number format and use that to tell you how to interpret his input.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OzzymandiasAuthor Commented:
thanx for your tips.
Is it possible to get the regional settings from the user's pc? That way, I could use RegExp to change e.g. points to #, and commas to @, save that value and display the value according to the user's settings...
All this is of course very labourous, but, as my customer says: "it is possible in Access, so why not in Internet Explorer, that's also a Microsoft program"... So even though using radio buttons or forcing to use eiter european or english notation are the more logic way of doing things, this would mean an extra action for the users, which has to be avoided...
Ozzy
0
OzzymandiasAuthor Commented:
sorry, to clarify: Is it possible to get the regional settings from the user's pc USING JAVASCRIPT?

Ozzy
0
knightEknightCommented:
try this:

if ( navigator.systemLanguage=="en-us" )
{
   // use . as decimal
}
else
{
   // use , as decimal
}


if systemLanguage doesn't work try navigator.userLanguage
0
knightEknightCommented:
... assuming you only need this to work on IE, correct?
0
OzzymandiasAuthor Commented:
correct...

in jscript a point is always used as DECIMAL separator, correct?
0
RichieHindleCommented:
[knightEknight]
> if ( navigator.systemLanguage=="en-us" )

There are English-speaking countries other than the US that use 1,234.56 as their number format, you know.  8-)

Using the language is not going to work.  You could probably assume that any English-speaking country uses 1,234.56, but you can't assume the opposite.  Most African countries seem to use 1,234.56 - Afrikaans, Swahili and Zulu all do.  There are some languages that use spaces as thousands separators, eg. Latvian and Czech.

My preferred solution would be to ban thousand-separators and return an error if the input contains more than one non-digit character.
0
knightEknightCommented:
yes, I know ... I wasn't trying to slight anyone, I simply offered that as an example of what shows up in my browser.

>> in jscript a point is always used as DECIMAL separator, correct?

Yes, so if the language is "en-us" (or other . decimal languages, as Richie points out) then you can safely remove all commas from the string
Else, convert the comma to a dot
0
knightEknightCommented:
>> in jscript a point is always used as DECIMAL separator, correct?

actually, I don't know that for a fact.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

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.