Saturday, November 5, 2011

How to check English character or not with javascript

The following is the javascript in head tag.

<script type="text/javascript">
function isValidEnglishLetter(object) {
param = object.value;
re = /[^A-Za-z0-9_\s\t\n\,\.\'\\?\!\@\#\$\%\^\&\*\{\}\[\]\<\>\(\)\/\+\-\^\?\~\|\:]/;
result = re.test(param);
if (!result) {
return true;
else {
alert("Only English characters and numerics are allowed.");
object.value = '';;
return false;

Here is in form.

<form id="form1" runat="server">
<asp:textbox id="txtNumber" runat="server" MaxLength="20" Width="100px" AutoCompleteType="Disabled"></asp:textbox>
<asp:Button ID="btnClick" runat="server" Text="Click"/>

In code behind,

protected void Page_Load(object sender, EventArgs e)
//For Keyup on textbox
txtNumber.Attributes.Add("OnKeyUp", "isValidEnglishLetter(this)");
//Otherwise, for button click
btnClick.Attributes.Add("OnClick", "isValidEnglishLetter(txtNumber)");

It will show the Alert message whenever you type other character(such as Japan character).

No comments:

Post a Comment