Forum : Text box Validation when it have onfocus and onblur event
Brief description  about Online courses   join in Online courses
View Santosh  Hegde 's Profile

Text box Validation when it have onfocus and onblur event

Dear sir,
i posted here two types of search boxes, both are linked with google.
first one have onsubmit validation when the empty text in the box,
and second one have Default text with onfocus and onblur events.
now the problem is how can i include onsubmit validation for second one? that should be act when it have onfocus default text in textaria or empty text in the box?

Here is the code:
<html>

<head>

<title>A Simple Form with JavaScript Validation</title>

<script type="text/javascript">

<!--

function validate_form ( )
{
valid = true;

if ( document.search_form.q.value == "" )
{
alert ( "Please fill something in the box, then 'Search' " );
valid = false;
}

return valid;
}

//-->

</script>

</head>

<body bgcolor="#FFFFFF">

<!-- Search box-1 -->

<form name="search_form" method="get"
action="http://www.google.com/search" target="_blank"
onSubmit="return validate_form ( );">


<fieldset style="width:400px;">
<p>Search On Google: <input type="text" name="q">
<input type="submit" name="send" value="Search"></p>
</fieldset>
</form>

<!-- Search box-2 -->

<form name="search" method="get" action="http://www.google.com/search" target="_blank" >
<fieldset style="width:400px;">
<input style="color: #4D4D4D;" name="q" id="header-search-text" onfocus="this.value=''" value="Search On Google" onblur="this.value=this.value==''?'Search On Google':this.value;" type="text">
<input type="submit" id="header-search-submit" value="Search" />
</fieldset>
</form>
</body>

</html>

looking forward to you..
please guide me..


with thanks
Asked by Santosh Hegde | Jan 12, 2011 |  Reply now
Replies (2)
View santosh hegde 's Profile
Thank you very much sir,
i changed my code like this and its working now:

<html>

<head>

<title>A Simple Form with JavaScript Validation</title>

<script type="text/javascript">

<!--
function validate_Search()
{
valid = true;

if (( document.mysearch.q.value == "" )||(document.mysearch.q.value == "Search On Google"))
{
alert ( "Please enter your Search string in the box" );
document.mysearch.q.focus();
valid = false;
}

return valid;
}

//-->

</script>

</head>

<body bgcolor="#FFFFFF">
<form name="mysearch" method="get" action="http://www.google.com/search" target="_blank" onSubmit="return validate_Search();">
<fieldset style="width:400px;">
<input style="color: #4D4D4D;" name="q" id="header-search-text" value="Search On Google" onfocus="this.value=(this.value==this.defaultValue)?'':this.value;" onblur="this.value=(this.value=='')?this.defaultValue:this.value;" type="text">
<input type="submit" id="header-search-submit" value="Search" />
</fieldset>
</form>
</body>

</html>

Mar 4, 2011
View teacher siliconindia 's Profile
Hi Santosh,

in that case u just validate both cases,
Blank field and field with default text.
Jan 31, 2011