How to Focus the asp:TextBox after validation

Posted by mareesnectar-12158 under JavaScript on 10/13/2011 | Points: 10 | Views : 4913 | Status : [Member] | Replies : 19
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style1
{
height: 12px;
}
.style2
{
width: 180px;
}
.style3
{
height: 12px;
width: 180px;
}
</style>

<script type="text/javascript">

function LengthCheck(txt)
{


var at = txt.value.length;
if (at != 10)
{
alert("Enter The last 10 characters");
// txtValue.focus();
}

}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


<asp:TextBox ID="txtValue" runat="server" onblur="LengthCheck(this)"/>
<asp:TextBox ID="txtValue1" runat="server"/>
</asp:Content>

The above code check the txtValue length successfully... but after showing the alert box ...The cursor goes to the next textbox(txtValue1)...
so i used txtValue.focus() ....this time it will throw the JavaScript Exception....then i used Document ElementById(),Document_form()....it will give same error..
Give me solution ....
Thanks




Responses

Posted by: Syedshakeer on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
To focus use the below code


document.getElementById("txtValue").focus();

Hope it helps you


Syed Shakeer Hussain

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: mareesnectar-12158 on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
I used
document.getElementById("txtValue").focus();
that time it throws
Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object Error...

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Syedshakeer on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
try with this

document.getElementById'<%=txtValue.ClientID%>').focus();


Syed Shakeer Hussain

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
document.getElementById('<%=textboxname.ClientID%>').focus();

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: mareesnectar-12158 on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
i tried
document.getElementById'<%=txtValue.ClientID%>').focus();
At Run time Registration.aspx(dynamic) shows the error line
<input name="ctl00$ContentPlaceHolder1$txtValue" type="text" id="ContentPlaceHolder1_txtValue" onblur="LengthCheck(this)" />

Microsoft JScript runtime error: Object expected error occured

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
document.getElementById('<%=textboxname.ClientID%>').focus()=true

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: mareesnectar-12158 on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down


document.getElementById('<%=textboxname.ClientID%>').focus()=true

its Not work......





mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Try to write this condition in if loop...
If u do so,it s strongly recommended for cursor to stay in current textbox not to jump out of its way

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: mareesnectar-12158 on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
yes.... but.......................
<script type="text/javascript">

function LengthCheck(txt) {


var at = txt.value.length;
if (at != 10) {
alert("Enter The last 10 characters");
document.getElementById'<%=txtValue.ClientID%>').focus()=true;
}


}
</script>


its didnt work


mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
@Marees.nectar@gmail.com

You have written everything correctly.
just change this commented [// txtValue.focus(); ] code to
txt.focus(); 

As you are passing the textbox object as txt , you should call txt.focus().

It will work.
Thanks,
Debata

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: mareesnectar-12158 on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Any one reply me


mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Please replace the commented code to txt.focus() as i have said earlier
function LengthCheck(txt)

{
var at = txt.value.length;
if (at != 10)
{
alert("Enter The last 10 characters");
txt.focus();
}
}


mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: mareesnectar-12158 on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Thank you very much.... Ndebata .....Now its work....

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Please mark it as answer ,
else DNF members may reply to this post.

Thanks,
Debata

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gausi on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
function LengthCheck(txt)

{


var at = txt.value.length;
if (at != 10)
{
alert("Enter The last 10 characters");
// txtValue.focus();
}

}


i think in the above function txt is itself a textbox object,
txt.focus();
will work for u.

Regards,

Gaurav Singh
(gausi)

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: mareesnectar-12158 on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Yes...The Below Code is working Gusi.....
function LengthCheck(txt)

{





var at = txt.value.length;

if (at != 10)

{

alert("Enter The last 10 characters");
txt.value=' ';
txt.focus();

}



}

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gausi on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
if works then please mark it as answer..

Regards,

Gaurav Singh
(gausi)

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bravi on: 10/15/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Your using master page so for getting control id we have to use below syntax

var txt = document.getElementById('<%=txtValue.ClientID%>')

try below function it will work

<script type="text/javascript">

function LengthCheck(txt)
{
var txt = document.getElementById('<%=txtValue.ClientID%>')
var at = txt.value.length;
if (at != 10)
{
alert("Enter The last 10 characters");
txt.focus();
}

}
</script>

Thanx,
Ravi B

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Godly77 on: 10/19/2011 [Member] Starter | Points: 25

Up
0
Down
Its simple just try

this.focus();



<script type="text/javascript">

function LengthCheck(txt) 

{

var at = txt.value.length;
if (at != 10)
{
alert("Enter The last 10 characters");
txt.focus( );
}

}

</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


<asp:TextBox ID="txtValue" runat="server" onblur="LengthCheck(th is) "/>
<asp:TextBox ID="txtValue1" runat="server"/>
</asp:Content>

Godly Mathew

mareesnectar-12158, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response