/*-----onblur and onclick function using contentplaceholder-----*/
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript" language="javascript">
function GetTextValue()
{
var box1 = document.getElementById('<%= tbText1.ClientID %>');
var box2 = document.getElementById('<%= tbText2.ClientID %>');
box2.value = box1.value;
}
</script>
<asp:TextBox ID="tbText1" runat="server" Columns="40" Rows="10" TextMode="MultiLine" onblur="GetTextValue();" ></asp:TextBox>
<asp:TextBox ID="tbText2" runat="server" Columns="40" Rows="10" TextMode="MultiLine" onclick="GetTextValue();"></asp:TextBox>
</asp:Content>
*********************
<div>
<script type="text/JavaScript">
function Calculate() {
var q = parseInt(document.getElementById("textbox1").value);
var r = parseInt(document.getElementById("textbox2").value);
document.getElementById("textbox3").value = q + r;
}
</script>
<asp:TextBox ID="textbox1" OnChange="Calculate()" runat="server"></asp:TextBox>
<asp:TextBox ID="textbox2" OnChange="Calculate()" runat="server"></asp:TextBox>
<asp:TextBox ID="textbox3" Text="0" runat="server"></asp:TextBox>
</div>
ContentTemPlate
var q = parseInt(document.getElementById('<%=TextBox1.ClientID%>').value);
var r = parseInt(document.getElementById('<%=TextBox2.ClientID%>').value);
*************************************************
<script type="text/JavaScript">
function
Calculate()
{
var
q = parseInt(document.getElementById('<%=txtPrice.ClientID%>').value);
var
r = parseInt(document.getElementById('<%=txtDiscount.ClientID%>').value);
var
s=q-(q * r/100);
//alert(s);
document.getElementById('<%= txtTotle.ClientID %>').value
=s.toString();
}
</script>
<asp:TextBox ID="txtDiscount"
onblur="Calculate();"
runat="server"
CssClass="txtbox1"
TabIndex="7"
Height="16px"></asp:TextBox>
<asp:TextBox ID="txtTotle" runat="server" onclick="Calculate();"
CssClass="txtbox1"
TabIndex="7"
Height="16px"></asp:TextBox>
/*************************************************/
only character values
<script type="text/javascript" language="javascript">
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return true;
return false;
}
</SCRIPT>press="return isNumberKey(event)"></asp:TextBox>
<asp:TextBox ID="TextBox1" onkeypress="return isNumberKey(event)" runat="server"></asp:TextBox></div>
only first and second and more values capital
<script type="text/javascript" language="javascript">
function Trim(strIn) {
strOut = strIn;
strOut = strOut.replace(/^ */g, "");
strOut = strOut.replace(/ *$/g, "");
return strOut;
}
function TitleCase(oField) {
var myValue = oField.value
var htext = Trim(myValue);
htext = htext.toLowerCase();
htext = htext.substr(0, 1).toUpperCase() + htext.substring(1, htext.length);
for (var i = 1; i < htext.length; i++) {
if (htext.substr(i, 1) == " ") {
while (htext.substr(i, 1) == " ")
i++;
if (i + 1 < htext.length)
htext = htext.substr(0, i) + htext.substr(i, 1).toUpperCase() + htext.substring(i + 1, htext.length);
else
htext = htext.substr(0, i) + htext.substr(i, 1).toUpperCase();
}
}
return htext;
}
</SCRIPT>
<asp:TextBox ID="txtArea1" onblur="this.value=TitleCase(this);" runat="server" CssClass="txtarea1"
TextMode="MultiLine" TabIndex="2"></asp:TextBox>
</td>
maxmum character in text box
<script language="javascript" type="text/javascript">
function checkTextAreaMaxLength(textBox,e,length)
{
var mLen = textBox["MaxLength"];
if(null==mLen)
mLen=length;
var maxLength = parseInt(mLen);
if(!checkSpecialKeys(e))
{
if(textBox.value.length > maxLength-1)
{
if(window.event)//IE
e.returnValue = false;
else//Firefox
e.preventDefault();
}
}
}
function checkSpecialKeys(e)
{
if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
return false;
else
return true;
}</script>
============================================================
<script type="text/javascript" language="javascript">
function checkTextAreaMaxLength(textBox,e,length)
{
var mLen = textBox["MaxLength"];
if(null==mLen)
mLen=length;
var maxLength = parseInt(mLen);
if(!checkSpecialKeys(e))
{
if(textBox.value.length > maxLength-1)
{
if(window.event)//IE
e.returnValue = false;
else//Firefox
e.preventDefault();
}
}
}
function checkSpecialKeys(e)
{
if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
return false;
else
return true;
}</script>
====================================
<asp:TextBox ID="TextBox1" runat="server" Height="30px" TextMode="MultiLine"
Width="175px" onkeydown="checkTextAreaMaxLength(this,event,'500');" ></asp:TextBox>
================================
web config :- visual studio:-2010
<machineKey validationKey="1619AB2FDEE6B943AD5D31DD68B7EBDAB32682A5891481D9403A6A55C4F91A340131CB4F4AD26A686DF5911A6C05CAC89307663656B62BE304EA66605156E9B5" decryptionKey="C9D165260E6A697B2993D45E05BD64386445DE01031B790A60F229F6A2656ECF" validation="SHA1" decryption="AES" />
***********
Javascript
Picking value from text box in JavaScript (for HTML)
/*----------------------------------------------------*/
<html>
<head>
<SCRIPT language = JavaScript>
function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C = (A + B)
document.frmOne.txtThirdNumber.value = C
}
</SCRIPT>
</head>
<body>
<FORM NAME = frmOne>
Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">
Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">
<P>
Total: <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>
</FORM>
</body>
</html>
ASP.NET not blank validation by 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 runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function KeyUpEvent()
{
if (document.getElementById("TextBox1").value=="")
{
alert("textbox can not be blank");
document.getElementById("TextBox1").focus();
return false;
}
if (document.getElementById("TextBox2").value=="")
{
alert("textBox can not be blank");
document.getElementById("TextBox2").focus();
return false;
}
var txt1 = document.getElementById("TextBox1");
var txt2 = document.getElementById("TextBox2");
var txt3 = document.getElementById("TextBox3");
if ((txt1.value != "") && (txt2.value != ""))
{
txt3.value = parseInt(txt1.value) + parseInt(txt2.value);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 332px">
Enter First Number :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Enter Second Number:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
Result
:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Show Result:" OnClientClick=" return KeyUpEvent()" />
<br />
</div>
</form>
</body>
</html>
Javascript validation for convert into upper case
/*-------------------------------------------------------------*/
function upperCase()
{
var x=document.getElementById("fname").value
document.getElementById("fname").value=x.toUpperCase()
}
</script>
Enter your name: <input type="text" id="fname" onblur="upperCase()">
/*----------------------------------------------------*/
<html>
<head>
<SCRIPT language = JavaScript>
function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C = (A + B)
document.frmOne.txtThirdNumber.value = C
}
</SCRIPT>
</head>
<body>
<FORM NAME = frmOne>
Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">
Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">
<P>
Total: <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>
</FORM>
</body>
</html>
ASP.NET not blank validation by 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 runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function KeyUpEvent()
{
if (document.getElementById("TextBox1").value=="")
{
alert("textbox can not be blank");
document.getElementById("TextBox1").focus();
return false;
}
if (document.getElementById("TextBox2").value=="")
{
alert("textBox can not be blank");
document.getElementById("TextBox2").focus();
return false;
}
var txt1 = document.getElementById("TextBox1");
var txt2 = document.getElementById("TextBox2");
var txt3 = document.getElementById("TextBox3");
if ((txt1.value != "") && (txt2.value != ""))
{
txt3.value = parseInt(txt1.value) + parseInt(txt2.value);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 332px">
Enter First Number :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Enter Second Number:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
Result
:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Show Result:" OnClientClick=" return KeyUpEvent()" />
<br />
</div>
</form>
</body>
</html>
Javascript validation for convert into upper case
/*-------------------------------------------------------------*/
function upperCase()
{
var x=document.getElementById("fname").value
document.getElementById("fname").value=x.toUpperCase()
}
</script>
Enter your name: <input type="text" id="fname" onblur="upperCase()">
Javascript validation for convert into upper case on keyup event
/*-------------------------------------------------------------------------------*/
<script language=javascript>
function upper(ustr)
{
var str=ustr.value;
ustr.value=str.toUpperCase();
}Type Lower-case Letters<textarea name="address" onkeyup="upper(this)">
function upper(ustr)
{
var str=ustr.value;
ustr.value=str.toUpperCase();
}Type Lower-case Letters<textarea name="address" onkeyup="upper(this)">
Javascript validation for convert into lower case on keyup event
/*-------------------------------------------------------------------------------*/
function lower(ustr)
{
var str=ustr.value;
ustr.value=str.toLowerCase();
}
{
var str=ustr.value;
ustr.value=str.toLowerCase();
}
Type Upper-case Letters<textarea name="address" onkeyup="lower(this)">
Javascript for email validation checking
/*---------------------------------------------*/
function validateForm()
{
var x=document.forms["myForm"]["email"].value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
-------------------------------------
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
Javascript for email validation checking
/*---------------------------------------------*/
function validateForm()
{
var x=document.forms["myForm"]["email"].value
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
}
-------------------------------------
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
/*---------------Numeric validation-------------*/
<SCRIPT language=Javascript>
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
//-->
</SCRIPT>
<asp:TextBox ID="TextBox1" onkeypress="return isNumberKey(event)" runat="server" ></asp:TextBox>
/*---------------string validation-------------*/
function chkValid2()
{
var Ename = document.getElementById("TxtEmpName");
var EnamePatrn = /^[A-Za-z]+$/;
if (!EnamePatrn.test(Ename.value))
{
alert("Enter Characters Only");
return false;
}
else
return true;
}
</script>
/*----- Toolkit Indication--------*/
onblur="messagehide('id1');"onfocus="messagedisplay('id1');"
<DIV style="DISPLAY: none" id="id1"><FONT face="Verdana" color=red size=1>Please specify the bussiness name.</FONT></DIV>
/*---Javascript Function--*/
function messagehide(msg)
{
document.getElementById(msg).style.display = 'none';
}
function messagedisplay(msg)
{
document.getElementById(msg).style.display = 'block';
}
/*-------------wordcount---------------------*/
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wordCount.aspx.cs" Inherits="wordCount" %>
<!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/jscript">
function countWords()
{
document.form1.wordcount.value = document.form1.inputString.value.split(' ').length;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input name="wordcount" type="text" value="" size="6" style="width: 18px"><br />
<asp:TextBox ID="inputString" runat="server" TextMode="MultiLine" onkeyup="countWords();"></asp:TextBox>
</form>
</body>
</html>
/*------First Letter UperrCase----*/
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script language="JAVASCRIPT">
function capitalizeMe(obj) {
val = obj.value;
newVal = '';
val = val.split(' ');
for (var c = 0; c < val.length; c++) {
newVal += val[c].substring(0, 1).toUpperCase() +
val[c].substring(1, val[c].length) + ' ';
}
obj.value = newVal;
}
</script>
</head>
<body>
<form id="Form2" runat="server">
<asp:TextBox ID="TextBox1" runat="server" onblur="capitalizeMe(this)"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</form>
</body>
</html>
onblur="messagehide('id1');"onfocus="messagedisplay('id1');"
<DIV style="DISPLAY: none" id="id1"><FONT face="Verdana" color=red size=1>Please specify the bussiness name.</FONT></DIV>
/*---Javascript Function--*/
function messagehide(msg)
{
document.getElementById(msg).style.display = 'none';
}
function messagedisplay(msg)
{
document.getElementById(msg).style.display = 'block';
}
/*--------CommonValidations--------*/
<script language="javascript" type="text/javascript">
function checkTextAreaMaxLength(textBox,e, length)
{
var mLen = textBox["MaxLength"];
if(null==mLen)
mLen=length;
var maxLength = parseInt(mLen);
if(!checkSpecialKeys(e))
{
if(textBox.value.length > maxLength-1)
{
if(window.event)//IE
e.returnValue = false;
else//Firefox
e.preventDefault();
}
}
}
function Typing(textBox)
{
alert("Select date from calender beside.");
}
function ValidDate(textBox,msg)
{
convert_date(textBox,msg);
messagedisplay(msg);
}
function checkSpecialKeys(e)
{
if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
return false;
else
return true;
}
function messagedisplay(msg)
{
document.getElementById(msg).style.display = 'block';
}
function messagehide(msg)
{
document.getElementById(msg).style.display = 'none';
}
var temp = 'dummy';
function LTrim( value )
{
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim( value )
{
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value )
{
return LTrim(RTrim(value));
}
function CheckIDmessagehide(msg,textbox)
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
return false;
}
}
}
document.getElementById(msg).style.display = 'none';
var take = textbox.value;
var a = trim(take);
if(a!="")
{
var PickArr = a.split(" ");
if(PickArr.length>1)
{
alert("Loginid does not contains the blank spaces");
textbox.value="";
textbox.focus();
}
}
if(a !="")
{
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState<4)
{
document.getElementById(msg).style.display = 'block';
if (temp == msg)
{
temp = msg;
}
loginidmsg.innerHTML="<table border='0' width='200' cellspacing='0' cellpadding='0' id='table1'><tr><td width='4'><img src='../images/images/loading.gif'></td><td><p align='left'><font color='black' face='verdana' size='1'> Searching.....</font></td></tr></table>";
}
else if(xmlHttp.readyState==4)
{
document.getElementById(msg).style.display = 'block';
if (temp == msg)
{
temp = msg;
}
if(xmlHttp.responseText=="found")
{
loginidmsg.innerHTML="<table border='0' width='200' cellspacing='0' cellpadding='0' id='table1'><tr><td width='4'><img src='../images/images/smallfail.gif'></td><td><p align='left'><font color='red' face='verdana' size='1'> Sorry! Id Already Exists</font></td></tr></table>";
textbox.focus();
}
else if(xmlHttp.responseText=="notvalid")
{
loginidmsg.innerHTML="<table border='0' width='200' cellspacing='0' cellpadding='0' id='table1'><tr><td width='4'><img src='../images/images/smallfail.gif'></td><td><p align='left'><font color='red' face='verdana' size='1'> Id ! Not Valid</font></td></tr></table>";
textbox.focus();
}
else
loginidmsg.innerHTML="<table border='0' width='200' cellspacing='0' cellpadding='0' id='table1'><tr><td width='4'><img src='../images/images/smallsuccess.gif'></td><td><p align='left'><font color='green' face='verdana' size='1'> Id is available</font></td></tr></table>";
}
}
var path="checklogin.aspx?LoginID="+ textbox.value;
xmlHttp.open("GET",path,true);
xmlHttp.send(null);
}
}
</script>
==================================================================================
<head runat="server">
<title>Untitled Page</title>
<script language="javascript">
function DisableRightClick(event)
{
//For mouse right click
if (event.button==2)
{
alert("Right Clicking not allowed!");
}
}
function DisableCtrlKey(e)
{
var code = (document.all) ? event.keyCode:e.which;
var message = "Ctrl key functionality is
disabled!";
// look for CTRL key press
if (parseInt(code)==17)
{
alert(message);
window.event.returnValue = false;
}
}
</script>
</head>
Now use this function on the textbox which we want to
disable copy paste and right clicking
<body>
<form id="form1" runat="server">
<div>
<strong>
Right click disabled</strong> textbox
<br />
<asp:TextBox ID="TextBoxCopy"
runat="server"
onMouseDown="DisableRightClick(event)">
</asp:TextBox><br />
<br />
<strong>Ctrl key </strong>disabled<br />
<asp:TextBox ID="TextBox2"
runat="server"
onKeyDown="return DisableCtrlKey(event)">
</asp:TextBox><br />
<br />
Another method to disable<strong> Cut,Copy and paste
</strong>in textbox<br />
<br />
<asp:TextBox ID="TextBox1"
runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
</asp:TextBox>
</form>
</body>
<!DOCTYPE html>
<html>
<head>
<script>
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
}
function moveWin()
{
myWindow.moveTo(0,0);
myWindow.focus();
}
</script>
</head>
<body>
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
<br><br>
<input type="button" value="Move 'myWindow'" onclick="moveWin()" />
</body>
</html>
=====================================================================
Page Reload
<script type="text/javascript" language="JavaScript">
function readCookie(name) {
return (document.cookie.match('(^|; )' + name + '=([^;]*)') || 0)[2]
}
</script>
<body onscroll="document.cookie='ypos=' + window.pageYOffset" onload="window.scrollTo(0,readCookie('ypos'))">
.................................................................................................................................................. <script type="text/javascript" language="javascript">
function CallPrint() {
var prtContent = document.getElementById("pop-details_main");
var WinPrint = window.open('', '', 'letf=0,top=0,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
}
</script>
<input onclick="javascript:CallPrint();" type="button" value="Print" /></td>
Stop auto-fill in browsers for textbox
<form id="Form1" method="post" runat="server" autocomplete="off">
<asp:TextBox Runat="server" ID="txtConfidential" autocomplete="off"></asp:TextBox>
Disable right click on web page
<body oncontextmenu="return false;" >
Disable right click on images
<img alt="MyImage" src="../ImgLoc/2.png" oncontextmenu="return false;"/>
Show alert on right click
<script type="text/javascript">
function disableRightClick()
{
alert("Sorry, right click is not allowed !!");
return false;
}
</script>
<body oncontextmenu=" return disableRightClick();">
...
</body>
Date Time Modifications
<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt').innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>