Hi Friends, in this article I will explain about Watermark for Username and Password in JavaScript. I already explained in the previous articles about Marquee tag or How to Scroll Text From left to right or How to Move the Text in HTML,C#/VB.NET:Save the generated pdf directly to the server directory folder without user prompt in ASP.NET and How to open PDF File in Adobe Reader, not in Browser in ASP.NET using C#/VB.NET.
We may see Watermark textboxes in so many sites. Suppose we take the twitter site in login page and signup page it contains Watermark textboxes. Watermark is not working properly for the password if we take the Textmode as Password(TextMode="Password").For that i take the extra textbox txtTempPwd and write the code as following.
Take one web page(.aspx page), Copy and Paste the below code in your web page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Watermark Textboxs for username and Password Using JavaScript</title>
<script language="javascript" type="text/javascript">
function WaterMark(objtxt, event) {
var defaultText = "Username";
var defaultpwdText = "Password";
// Condition to check textbox length and event type
if (objtxt.id == "txtUserName" || objtxt.id == "txtPwd") {
if (objtxt.value.length == 0 & event.type == "blur") {
//if condition true then setting text color and default text in textbox
if (objtxt.id == "txtUserName") {
objtxt.style.color = "Gray";
objtxt.value = defaultText;
}
if (objtxt.id == "txtPwd") {
document.getElementById("<%= txtTempPwd.ClientID %>").style.display = "block";
objtxt.style.display = "none";
}
}
}
// Condition to check textbox value and event type
if ((objtxt.value == defaultText || objtxt.value == defaultpwdText) & event.type == "focus") {
if (objtxt.id == "txtUserName") {
objtxt.style.color = "black";
objtxt.value = "";
}
if (objtxt.id == "txtTempPwd") {
objtxt.style.display = "none";
document.getElementById("<%= txtPwd.ClientID %>").style.display = "";
document.getElementById("<%= txtPwd.ClientID %>").focus();
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><b>UserName:</b></td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Text="Username" Width="150px" ForeColor="Gray" onblur = "WaterMark(this, event);" onfocus = "WaterMark(this, event);" />
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td>
<asp:TextBox ID="txtTempPwd" Text="Password" runat="server" onfocus="WaterMark(this, event);" Width="150px" ForeColor="Gray" />
<asp:TextBox ID="txtPwd" TextMode="Password" Text="Password" runat="server" Width="150px" Style="display:none" onblur="WaterMark(this, event);"/>
</td>
</tr>
</table>
</form>
</body>
</html>
Then the output like below.
when we click on the textboxes then the text will disappear like below.
Best ASP.NET 4.6 Hosting Recommendation
ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers ASP.NET hosting starts from $1/month only. They also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable ASP.NET Hosting, ASPHostPortal.com should be your best choice.