Click here to Skip to main content
15,887,485 members
Articles / Programming Languages / Javascript
Tip/Trick

JavaScript Arabic Alert Message

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
29 Sep 2014CPOL2 min read 17.8K   4   2
Easy way to show JavaScript Alert Message having Arabic Text

Introduction

This is a tip where developers find an easy way to show up JavaScript Alert Messages having Arabic Text. While developing a web site which supports multiple language, many times, it is a requirement to show Alerts having Arabic text. After several searches and discussions with others, I could find a way to use Regular Expressions to address the issue.

JavaScript Arabic Alert Message

Think:

Someone is elegant, smart in writing server and client side code for any web application. We are great and we do great jobs. As every one knows, in generic sites, there always is some client side validation that uses JavaScript to validate and display alters. We do have one application that uses classic ASP 3.0 and .NET that too uses client side validation using JavaScript. Of late, a requirement came up to support additional language for the site which includes Arabic. There are smarter developers who do things differently which is awesome, but I am not from that la cream de la cream group. We started thinking of using some open source libraries to show alert messages in Arabic. That is possible and one can have resource files created for globalization in ASP.NET web pages and implement.

But what about classic ASP? I started scratching my head if I could have a very quick solution for showing alerts in Arabic and searched hither and thither and could not find a glue that fixed my broken parts in JavaScript message. One fine day, we found a quick fix and implemented and stretched my legs. It was a nice piece of work and I could not stop without sharing it for others.

What did we do:

Simple. You can do it too. Open up any editor of your choice to write a simple demo.html file and inject the following lines and save as demo.html.

Using the Code

HTML
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>A demo of Arabic Alert</title>
    <script type="text/javascript" language="javacsript">
        function UnicodeAlert() {
            var str;
            str = document.getElementById('inputElement').value;
            alert(str.replace(/&#(\d+);/g));
            return false;
        }
    </script>
</head>
<body>
    <h1> A demo of Arabic Alert </h1>
    <div style="margin: 10px 0 20px 0;">
        <div style="border:solid 1px ">Hello تعلمت العربية</div>
        <div>Hell'o تعلمت العربية</div>
    </div>
    <input type="text" id="inputElement" value="hello">
    <input type="button" onclick="return UnicodeAlert();" value="Click" />
</body>
</html>

Make sure the IIS is running on your local box. To do that, open Internet Explorer browser and paste http://localhost/, you would notice IIS logo if that is running or some message. If you have never configured IIS, you need to take help from some one to configure on your local system, do that, it is not a very difficult task.

Go to C:\inetpub\wwwroot and create a Demo folder and save the above demo.html with the code written and create a virtual directory and point to that.

Now, everything is cooked and ready to taste.

Open Internet Explorer browser and and check. Since I have one demo virtual directory having the demo.html, I used URL like http://localhost/demo/Demo.html.

There are two lines having some test text below the heading “A demo of Arabic Alert”.

Copy one line, paste onto the input box and hit button “Click” and see the alert for Arabic text.

Do the same for other line and check.

Useful? No, never, not much. But I am happy I could share what I thought.

Rajendra Kumar Sahu
Mindfire Solutions

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)2-Oct-14 5:43
Shemeemsha (ഷെമീംഷ)2-Oct-14 5:43 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun29-Sep-14 17:21
Humayun Kabir Mamun29-Sep-14 17:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.