How To Use Watermark In ASP.NET Textbox Using JavaScript

Step 1
 
In this blog, we will show you how can you display a watermark in ASP.NET of a textbox, using JavaScript.
 
Step 2
 
Prior to it, we need to create a new ASP.NET application and add a textbox control on the page. For this, create a new ASP.NET code, which is given below.
  1. <div>    
  2.     Username :<asp:textbox ID="txtUserName" Text="Use ; as a separator" Font-Bold="true" onfocus="WaterMarkFocus(this,'Enter the username')" onblur="WaterMarkBlur(this,'Enter the username')"  runat="server" ForeColor="gray" ></asp:textbox>        
  3. </div>   
Step 3
 
Subsequently, we will add a JavaScript Code to display a watermark on the textbox given below. 
  1. <script type="text/javascript">    
  2.     function WaterMarkFocus(txt, text) {    
  3.         if (txt.value == text) {    
  4.             txt.value = "";    
  5.             txt.style.color = "black";    
  6.         }    
  7.     }    
  8.      
  9.     function WaterMarkBlur(txt, text) {    
  10.         if (txt.value == "") {    
  11.             txt.value = text;    
  12.             txt.style.color = "gray";    
  13.         }    
  14.     }    
  15. </script>    
Step 4
 
Now, JavaScript function is used as a parameter value, which will be used as a watermark textbox.
 
Step 5
 
When a user will focus on a  textbox, the watermark text will disappear and on removing the mouse, the water mark text will appear. In some cases, until the user types, it will remain the same.
 

Conclusion

 
Now, the water mark textbox is created, using JavaScript. Feel free to comment with some suggestions. Thank you.