Sunday, October 25, 2009

Clear default text onClick - restore if nothing entered

Let's write a JavaScript function which will clear the "default" text in a field when the user clicks into it and the second function will replace the default text in the field if the field was left blank.

Put this inside the <head> </head> tag:

<script type="text/javascript">
function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
</script>

Then add the following onclick, onblur events to your field:

<asp:TextBox ID="rajTextBox" runat="server" Text="Rajendra Sedhain"
Width="369px" Height="18px" onclick="clickclear(this, 'Rajendra Sedhain')" onblur="clickrecall(this,'Rajendra Sedhain')"></asp:TextBox>

Happy Programming !!!

Monday, October 5, 2009

Disabling an ASP.Net Validator through Javascript

Let say, I have two dropdown lists, both have required field validation controls and use Javascript to hide the second dropdown when choose the specific items from the first dropdown list. It works fine but the problem is when choose the specific items from the first dropdown it hides the second one but it still displays the required field validator.

So, the solution:

When we hide, we can disable the validator at the same time using ValidatorEnable().

function validate()
{

var myValidator = document.getElementById('myValidatorClientID');
ValidatorEnable(myValidator, false);

}

To make enable replace that 'false' by 'true'.

Happy Programming !!!