Watermark the textbox using javascript

Posted by Venkat | Labels: ,

Hi , Now we have to discuss about the watermark the textbox using javascript

for that you have to use onblur and onfocus property of your textbox


<script type = "text/javascript">

var defaultText = "Enter your text here";

function WaterMark(txt, evt)

{

if(txt.value.length == 0 && evt.type == "blur")

{

txt.style.color = "gray";

txt.value = defaultText;

}

if(txt.value == defaultText && evt.type == "focus")

{

txt.style.color = "black";

txt.value="";

}

}

</script>


Here is the HTML Code of the Textbox control

<asp:TextBox ID="TextBox1" runat="server" Text = "Enter your text here"

ForeColor = "Gray" onblur = "WaterMark(this, event);"

onfocus = "WaterMark(this, event);">

</asp:TextBox>

Now we have to write the attributes through code on your Page_load Event

TextBox1.Attributes.Add("onblur", "WaterMark(this, event);")

TextBox1.Attributes.Add("onfocus", "WaterMark(this, event);")

Textbox should allow minimum 5 Character

Posted by Venkat | Labels: ,

Hi Use this code.


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator
ID="valUserName" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" ErrorMessage="Minimum length 6 characters"
ForeColor="" ValidationExpression=".{6}.*" ></asp:RegularExpressionValidator>

PayOffers.in