Writing to a txt file through JavaScript
<script type="text/javascript">
function WriteTextFile() {
var fileName="d:\\test.txt";
var object= new ActiveXObject("Scripting.FileSystemObject");
var model = 8; // Append mode
var create = true; // Create if not exist
var format = 0; // ASCII mode
var file = object.OpenTextFile(fileName, model, create, format);
file.write("Welcome to www.DotNeter.com");
file.close();
}
</script>
Remove All Special Characters in a String using Java Script regular expression.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function RemoveSpecialCharacters() {
var temp = document.getElementById('<%=TextBox1.ClientID%>').value;
temp = temp.replace(/[^a-zA-Z 0-9]+/g, '');
alert(temp);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return RemoveSpecialCharacters();"/>
</form>
</body>
</html>
Counting Characters Realtime in ASP.NET TextBox Using JavaScript
ASPX file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function countCharacters() {
var count = parseInt(document.getElementById('<%=TextBox1.ClientID%>').value.length);
document.getElementById('<%=Label1.ClientID%>').innerHTML = 100 - count;
}
</script>
<style type="text/css">
.style1
{
color: #FF0000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Max 100 characters<br />
<span class="style1">Characters left:</span>
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
</body>
</html>
ASPX.CS file
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onKeyUp", "countCharacters()");
}
Counting textbox length with JavaScript
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function countCharacters() {
var count = document.getElementById('<%=TextBox1.ClientID%>').value.length;
alert(count);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return countCharacters();"/>
</form>
</body>
</html>
Related post:
Hide an ASP.NET control using JavaScript
function hideControl()
{
document.getElementById('<%=ControlName.ClientID%>').style.visibility = "hidden";
return false;
}
Show an ASP.NET control using JavaScript
function showControl()
{
document.getElementById('<%=ControlName.ClientID%>').style.visibility = "visible";
return false;
}