469,903 Members | 2,182 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Return focus to failed validation field after user clicks to get rid of error message

Hello frnds

I have created a webbased form using asp and javascript .. while submitting the form i am validating the fields in the form .it validates the field and returns alert message. but when user clicks on alert box it submits the form instead of focusing to desired field...

I have cascade style webform .so if havent filled field in 2nd sheet and i click on submit button in 4 sheet.. it prompts the alert box but does not focus to 2nd sheet field and instead it submits the form...

here is my part of my code please help me out.....

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function ValidatehostForm()
{
var Ctargetlength = document.form.storcurtar.value
var hostname = /[^a-zA-Z0-9]/g
var re = /[\D]/g
var nbu=/[^a-zA-Z0-9]/g

if (document.form.hostname.value == "")
{
window.alert("Please Enter Hostname in Host webpage");
document.form.hostname.focus();
return false;
}

if (hostname.test(document.form.hostname.value))
{
alert("Hostname should be aplha numeric");
document.form.hostname.focus();
return false;
}

if (document.form.lob.selectedIndex < 1)
{
alert("Select Lob From Dropdown list in Host webpage");
document.form.lob.focus();
return false;
}


return true;

}
</script>
<link rel="STYLESHEET" type="text/css" href="./css/sdmsstyle.css">
<link rel="stylesheet" type="text/css" href="./css/datepicker.css">
<style type="text/css">
td { padding:0px; }
table { padding:0px; }
</style><!--#include file="menu.asp"-->
<title></title>

</head>
<body>
<% if ttype= "NEW" then %>
<!--#include file="./includes/masthead.asp" -->
<table width="100%" cellpadding="0" cellspacing="0" align="center" border="0">
<tr>
<!--<form name="form" action="add_hostform.asp" method="post" onsubmit="return ValidatehostForm();">-->
<form name="form" action="add_hostform.asp" method="post" >
<td class="cardtext" style="padding-top:9px; padding-bottom:9px;">
<input class=button type=submit value=SAVE onclick ="return ValidatehostForm();" >
<input class=button type=button value=CANCEL onClick="window.location='hostMain.asp'">
</tr>
</table>
<div style="display: block;" id="summary">
<table>

</td>
<tr>
<td class="cardtext">
<table width="100%" cellpadding="0" cellspacing="0" align="center"
border="0">
<tr>
<!-- <TR><td><img src="./images/transparent.gif" height="24" width=1></td></TR> -->
</tr>

<tr>
<td class="tab_c2_left"></td>
<td class="tab_c2_center" width="55"><font class=
"xs_b_white_text">SUMMARY</font></td>
<td class="tab_c2_right"></td>
<td class="tab_white_left"></td>
<td class="tab_white_center" width="55"><a href="#" class=
"xs_b_grey_link" onclick=
"show('detail'); hide('summary');">DETAIL</a></td>
<td class="tab_white_right"></td>
<td class="tab_white_left"></td>
<td class="tab_white_center" width="55"><a href="#" class=
"xs_b_grey_link" onclick=
"hide('summary');show('storage');">STORAGE</a></td>
<td class="tab_white_right"></td>
<td class="tab_white_left"></td>
<td class="tab_white_center" width="55"><a href="#" class=
"xs_b_grey_link" onclick=
"hide('summary');show('comments');">COMMENTS</a></td>
<td class="tab_white_right"></td>
<td class="tab_white_left"></td>
</tr>

<tr>
<td height="6" colspan="19" class="c2_tint"></td>
</tr>

<tr>
<td colspan="4"></td>
</tr>
</table>
</td>
</tr>

<tr class="cardtext">
<td colspan="2" class="cardtext">HOSTNAME<br>
<input type="text" style='WIDTH: 150px;' name="hostname"></td>
</tr>

<tr class="cardtext">
<td colspan="2" class="cardtext">ALIAS<br>
<input type="text" style='WIDTH: 150px;' name="alias"></td>
</tr>

<tr class="cardtext">
<td colspan="2" class="cardtext">HOST TYPE<br>
<input type="text" style='WIDTH: 100px;' name="hosttyp" value="<%=prod%>"></td>
</tr>

<tr>
<td colspan="2" class="cardtext">LOB<br>
<%
rs.Open lobsel, connpen, 1, 2
Response.Write "<select style='WIDTH: 100px' name='lob'><option selected value=" & lob & ">" & lob & "</option>"
Do While NOT rs.EOF
lob = rs("lob_name")
Response.Write "<option value=" & lob & ">" & lob & "</option>" & vbcrlf
rs.MoveNext
Loop
rs.close
%> </td>
</tr>
</table>
</div>[/HTML]
Nov 22 '07 #1
1 2066
andho
34
1. your <form> tag doesnt seem to be closed.
2. the onsubmit="return ValidatehostForm();" attribute is not in the uncommented form tag, but it is correclty specified in the commented out tag. Maybe its an asp thing. (i dont know asp)
3. no need for "return ValidatehostForm();" in the onclick event of the save button
Nov 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Roger Withnell | last post: by
14 posts views Thread by dcassar | last post: by
7 posts views Thread by saurabh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.