Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 333
AMit SiNgh Avatar asked Jan 05 '23 23:01

AMit SiNgh


1 Answers

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
like image 80
gurvinder372 Avatar answered Jan 08 '23 15:01

gurvinder372