Wednesday, September 29, 2010

Remove letters and symbols from a string using Javascript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Removechar.aspx.cs" Inherits="Removechar" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function Replace()
{
var txt=document.getElementById('txtString').value;
var len=txt.length;
var i=0;
for(i=0;i<len;i++)
{
var num=ascii_value (txt[i])
if(num<48 || num>57)
{
txt=txt.replace(txt[i],"");
len=txt.length;
i--;
}
}
alert(txt);
}
function ascii_value (c)
{
c = c.charAt(0);
var i;
for (i = 0; i < 256; ++ i)
{
var h = i . toString (16);
if (h . length == 1)
h = "0" + h;
h = "%" + h;
h = unescape (h);
if (h == c)
break;
}
return i;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtString" runat="server" />
<asp:Button ID="btnSubmit" runat="server" onclientclick="Replace()" Text="Submit" />
</div>
</form>
</body>
</html>

No comments:

Post a Comment