[resolved]how to set focus for text box on submit pl be clear
how to set focus to text box?
--------------------------------------------------------------------------------
i am learning .net. In asp.net i run a web form that has a username and password field. If i press submit button without entering the username the focus must go back to the username textbox and show a message that i have not entered a username. please be very clear since i am a novice.can u write that code for me.
pl help me,
sai abhiram bandhakavi.
Re: how to set focus for text box on submit pl be clear
Why are you double-posting? I've answered this in your other thread.
Re: how to set focus for text box on submit pl be clear
i posted it again becos i did'nt understand it
Re: how to set focus for text box on submit pl be clear
Then all you need to do is ask.
You must use simple javascript form validation. Here is an example:
Code:
<script Language="Javascript">
<!--
// The previous line hides the script from old browsers that cant interpret it
function emailvalidation(entered, alertbox)
{
// E-mail-Validation (c) Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove the this line and the two lines above.
with (entered)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
}
function valuevalidation(entered, min, max, alertbox, datatype)
{
// Value-Validation (c) Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove the this line and the two lines above.
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}
function digitvalidation(entered, min, max, alertbox, datatype)
{
// Digit-Validation (c) Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove the this line and the two lines above.
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value); if (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}
function emptyvalidation(entered, alertbox)
{
// Emptyfield-Validation (c) Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove the this line and the two lines above.
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}
function formvalidation(thisform)
{
with (thisform)
{
if (emailvalidation(Email,"Illegal E-mail")==false) {Email.focus(); return false;};
if (valuevalidation(Value,0,5,"Value MUST be in the range 0-5")==false) {Value.focus(); return false;};
if (digitvalidation(Digits,3,4,"You MUST enter 3 or 4 integer digits","I")==false) {Digits.focus(); return false;};
if (emptyvalidation(Whatever,"The textfield is empty")==false) {Whatever.focus(); return false;};
}
}
// The next line causes oldfashion browsers to start interpreting the code again.
//-->
</script>
and in the form:
Code:
<form onsubmit="return formvalidation(this)" align="center" name="detectform"><table border="1"><tr><td align="right" bgcolor="#008080"><font color="#FFFFFF" face="Arial">EMAIL:</font></td><td><div align="left"><p><font face="Arial"><input type="text" name="Email" size="20" onChange="emailvalidation(this,'Invalid Email');"></font></td></tr><tr> <td align="right" bgcolor="#008080"><font color="#FFFFFF" face="Arial">VALUE (0-5):</font></td><td><font face="Arial"><input type="text" name="Value" size="20" onChange="valuevalidation(this, 0, 5,'Value MUST be an Integer in the range: 0-5');"></font></td></tr><tr><td align="right" bgcolor="#008080"><font color="#FFFFFF" face="Arial"> VALUE (Integer, 3-4 digits):</font></td><td><font face="Arial"><input type="text" name="Digits" size="20" onChange="digitvalidation(this, 3, 4,'You MUST enter 3 or 4 Integer Digits','Integer');"></font></td></tr><tr><td align="right" bgcolor="#008080"><font color="#FFFFFF" face="Arial"> Do not leave this field empty:</font></td><td><font face="Arial"><input type="text" name="Whatever" size="20"></font></td></tr><tr><td align="right" bgcolor="#008080" colspan="2"><font color="#FFFFFF"><div align="center"><center><p><font face="Arial"><input type="submit" value="Click here to validate all fields at once" name="Button1"></font></td></tr></table></form>
Try this out in an html page and you'll understand what's happening. To note here is the part I've highlighted in green.