Thursday, October 13, 2011

watermark textbox using javascript

Using Java script
  <script language="javascript" type="text/javascript">
        function WaterMark(txtName, event) {
            var defaultText = "Enter Username Here";
            // Condition to check textbox length and event type
            if (txtName.value.length == 0 & event.type == "blur") {
                //if condition true then setting text color and default text in textbox
                txtName.style.color = "Gray";
                txtName.value = defaultText;
            }
            // Condition to check textbox value and event type
            if (txtName.value == defaultText & event.type == "focus") {
                txtName.style.color = "black";
                txtName.value = "";
            }
        }
</script>




 <asp:TextBox ID="txtUserName" runat="server" Text="Enter Username Here" ForeColor="Gray"  onblur =  "WaterMark(this, event);" onfocus = "WaterMark(this, event);" />
                                       

 using AJAX Toolkit Control

 <asp:TextBox ID="txtUserName" runat="server"  ForeColor="Gray"   />
 <cc1:TextBoxWatermarkExtender ID="txt" runat="server" WatermarkText="hii enter here"  TargetControlID="txtUserName"></cc1:TextBoxWatermarkExtender>
                                   

No comments:

Post a Comment