Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsNan locale aware? 1

Status
Not open for further replies.

MrPeds

Programmer
Jan 7, 2003
219
GB
Hi,

I have another noob javascript question, sorry to be a pain!

I have a simple html table. Each row contains a text box where the user enters some percentages. The percentages must add up to 100%.

I have some validation code to ensure that only numbers are entered in the text boxes. What I would like to know is:

1) If a user in Europe enters a decimal like this 25,2 (for 25.2%) can JavaScripts IsNan able to tell what the locale is and verify that this is a valid number?

2) I have some code that adds up the numbers so that they equal 100%. Can JavaScript take account of the numbers locale and add numbers like 25,2 and 10 ?

Is it easier just to ensure that a user enters numbers such as 25.2 10 etc to make things easier?

Thanks,

MrPeds
 
I don't know. But, you could do something like this to avoid the problem. Regular expressions are my friend.

Code:
<script type="text/javascript">
var regexp = new RegExp('^(\\d{0,3})[,\\.]?(\\d*)%?$');
	var cnumber = "23,15".replace(regexp, '$1.$2%');
	alert(cnumber);
</script>
The regex will clean it for you, assuming it matches the pattern. You could remove the % and then check if its a number and within the acceptable range.

-----------------------------------------
I cannot be bought. Find leasing information at
 
Sorry for the late response!

This script is really powerful and simple, thank your for your help!

MrPeds
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top