65.9K
CodeProject is changing. Read more.
Home

JavaScript Farsi TextBox

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.96/5 (13 votes)

Jan 11, 2006

viewsIcon

82277

downloadIcon

1146

A TextBox for write Farsi

Introduction

On the internet some times we need get information from user in language other than English. for example we want the user type his name in Perisan language. there is two way to do this. First you can hope the user installed the farsi language in computer and change the language of windows to type.

This way have one Big problem and that is the Farsi Language that diffrent windows have is diffrent i mean. for Example the character codes in diffrent windows is diffrent.

The second way to handle both problems is using some javascript codes. in this way the user don't need to have Farsi Installed on machine. When the user type the code that user type convert to Farsi Language Unicode and write in the text box.

How do it ?

We must declare a function to convert typed character to Unicode.(Your page language must set to UTF-8).

function keyenter(field,e)
{
var key;
if (window.event)
   key = window.event.keyCode;
if (key>31)
  if (key<128)
  {
    if (window.event)
      window.event.keyCode=' !"#$%،گ)(×+و-./0123456789:ك,=.؟@ِذ}ىُيلا÷ـ،/’د×؛َءٍف‘{ًْإ~جژچ^_پشذزيثبلاهتنمئدخحضقسفعرصطغظ<|>ّ'.charCodeAt(key-32);
  }
}
Note: Remember that the characters must not convert to unicode numbers and must look likes what you see here.

In the definition of textbox you must call OnKeyPress event to the function you have declared.

<input onkeypress="keyenter(this,event)" type="text" name="T1" size="20">