1

This is my code:

$(".count").text(parseInt($(".count").text())+1);

the issue that this code working for English numbers only and not working with Arabic number.

1 in Arabic = ١

How can solve this issue to make this code working for both Arabic and English languages?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Try something like this String.fromCharCode(lastChar.charCodeAt(0) + 1); this might help you. – Shiladitya Jul 03 '17 at 06:24
  • 2
    js can't parse Arabic numeric. you need to first convert Arabic numeric to numeric and then add one and then convert it back to Arabic numeric. this is called localization – Shanil Fernando Jul 03 '17 at 06:24
  • What you call "Arabic numbers" is called "Eastern Arabic numbers" in Unicode set. – Alice Oualouest Jul 03 '17 at 06:25
  • you can use a hidden input field with english value, get the value from that hidden element, increment it by 1 and then convert it to arabic equivalent unicode character. – Avijit Jul 03 '17 at 06:26
  • FYI, all regular computer languages are based in the English language. That was definitively settled in the days of the Cobol spec. – Álvaro González Jul 03 '17 at 06:29
  • According to the [spec](http://www.ecma-international.org/ecma-262/6.0/#sec-tonumber), for JavaScript, only [0-9] are considered decimal digits. Maybe you need a 3rd party library that deals with this, although it's a bit of a niche use case so maybe there's none available. – apokryfos Jul 03 '17 at 06:33

3 Answers3

1

Use a normal number then convert to Arabic

var arNum = '۰۱۲۳۴۵۶۷۸۹', val = 0;
$(function() {
  val = arNum.indexOf($(".count").text()); // works only from 0 - 9
  $("#inc").on("click",function() {
    val++;
    if (val > 9) {
      val=0;
    }  
    $(".count").text(arNum.charAt(val));
  });  
})  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<span class="count">۰</span><button type="button" id="inc">Increment</button>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

I don't think parseInt supports UTF-8 strings. It has only two arguments one is a string numeral to be parsed and the other is the radix. parseInt(string, number).

Check if isNaN if it returns true then parseInt won't work.

Charles
  • 1,008
  • 10
  • 21
0

I think you'll need to do a string replacement after the addition. JavaScript, to my knowledge, has no built-in support for translating numbers to e.g. Arabic.

For example, here is a nifty answer to a similar question which contains a function for said replacement.

Basically, you create a lookup table for the Arabic numbers, and then run a String.replace over your incremented number (as a string).

vijoc
  • 683
  • 8
  • 17