AMit SiNgh
AMit SiNgh

Reputation: 325

using javascript how to check string contains hindi font?

I am uploading a file using input type file I want to check that the name contains hindi font or english font.

Below is my code.

<!doctype html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <script>
            $("document").ready(function () {

                $("#upload").change(function () {
                    var filename = $(this).val().replace(/^.*[\\\/]/, '')
                    alert(filename);

                });
            });
        </script>
    </head>
    <body>
        <div>
            <input type="file" name="upload" id="upload" value="upload" />
        </div>

    </body>
</html>

Upvotes: 4

Views: 4157

Answers (2)

gurvinder372
gurvinder372

Reputation: 68413

Hindi characters' character code is from 2309 to 2361.

try this

function hasHindiCharacters(str)
{
  return str.split("").filter( function(char){ 
    var charCode = char.charCodeAt(); return charCode >= 2309 && charCode <=2361;
  }).length > 0;
}

You can use this method

hasHindiCharacters("अasd"); //outputs true

hasHindiCharacters("asd"); //outputs false

DEMO

    function hasHindiCharacters(str)
    {
      return str.split("").filter( function(char){ 
        var charCode = char.charCodeAt(); return charCode >= 2309 && charCode <=2361;
      }).length > 0;
    }


    console.log(hasHindiCharacters("अasd")); //outputs true

    console.log(hasHindiCharacters("asd")); //outputs false

Upvotes: 11

Rick
Rick

Reputation: 1913

You should be able to loop through each of the characters in the file name. Within the loop, use filename.charCodeAt(index) to get the Unicode value of the character. Checking if the char value is between 0900 and 097F which contains all the characters in the Devanagari Unicode block should tell you if it's using Hindi.

Upvotes: 2

Related Questions