Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form submitted despite JS errors 1

Status
Not open for further replies.

GoldPearl

Programmer
Aug 9, 2005
64
i have a form with controls in it which i validated using Javascript. when i enter erroeous data, it displays the error message but also submit the form with the erroneous data. i want it to display an error msg like "please enter correct data" instead.

code for form is as follows:


<script language="javascript">

function checkWholeForm()
{
var add = "";
add += textonly();
add += checkDropdown();
return true;
}

//function to check alphabetic (Department name)

function textonly()
{
var checkOK="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var checkstr;
var allValid=true;
var allNum= ""

if (frmdeptsmaint.txtdeptname.value == "")
{
alert("Compulsory Field. Department Name cannot be blank.");
frmdeptsmaint.txtdeptname.focus( );
return false;
}
else
{
checkstr=frmdeptsmaint.txtdeptname.value;
for(i=0; i<checkstr.length;i++)
{
ch= checkstr.charAt(i);
for(j = 0;j<checkOK.length;j++)
if( ch== checkOK.charAt(j))
break;
if (j ==checkOK.length)
{
allValid = false
break;
}
if(ch!=",")
allNum+=ch;
}
if (!allValid)
{
alert("Please specify a correct Department Name.");
frmdeptsmaint.txtdeptname.select();
valid=false;
}
}
return true;

}

function checkDropdown()
{
if (frmdeptsmaint.lstpos.value == 0)
{
alert ("You didn't choose an option from the drop-down list.");
}
return false;
}


</SCRIPT>

</head>

<body bgcolor="#525C6D" text="#FFFFFF" link="#FFFFFF" vlink="#66FFFF" alink="#66FFFF">
<p align="right"><font color="#FFFFFF" face="Arial"><strong><img src="images/home.gif" width="24" height="24">
<font size="2"><a href="role.asp">Home </a></font></strong></font><img src="images/logout.ico" width="24" height="24"><a href="logout.asp"><font size="2" face="Arial">Logout</font></a></p>
<p align="left"><font size="2" face="Arial">Welcome <%=userid1%> </font></p>
<p>
<%
If request("error")="1" Then
Response.Redirect "dupadddept.asp"

Else If request("success")="1" then
response.Redirect "adddeptok.asp"
End If
End If
%>
</p>
<div align="center">
<p><strong><font face="Arial"> Department Maintenance</font></strong></p>
<form name="frmdeptsmaint" method="post" action="dept.asp">

<div align="center">
<table width="33%" border="2" cellspacing="20" bordercolor="#FFFFFF" bgcolor="#CCCCCC">
<tr>
<td bordercolor="#CCCCCC"><font color="#000000" size="2" face="Arial">Department
ID:&nbsp;</font></td>
<td bordercolor="#CCCCCC"><font color="#000000" size="2" face="Arial">
<input name="txtdeptid" type="text" id="txtdeptid" size="20" value='<%=id%>'>
</font></td>
</tr>
<tr>
<td bordercolor="#CCCCCC"><font color="#000000" size="2" face="Arial">Department
Name: </font></td>
<td bordercolor="#CCCCCC"><font color="#000000" size="2" face="Arial">
<input name="txtdeptname" type="text" id="txtdeptname" size="20">
</font></td>
</tr>
<tr>
<td bordercolor="#CCCCCC"><font color="#000000" size="2" face="Arial">Position:&nbsp;&nbsp;&nbsp;&nbsp;</font></td>
<td bordercolor="#CCCCCC"><font color="#000000" size="2" face="Arial">
<select name="lstpos" size="1" id="lstpos">
<option selected>Select a Position</option>
<%
do while not rspos.eof
%>
<option><%=rspos(0)%></option>
<%
rspos.movenext
loop
rspos.close
set rspos = nothing
cn.close
set cn = nothing
%>
</select>
</font></td>
</tr>
</table>
<p><font size="2" face="Arial">
<input name="btnadd" type="submit" id="btnadd" value="Add" onclick="checkWholeForm()">
<input name="btnview" type="submit" id="btnview" value="View">
</font></p>
</div>
</form>


code for ADD button:

Sub AddDept
dim rs
dim cn
dim sql
dim duplicate
dim namedept
dim iddept

namedept=request.Form("txtdeptname")
iddept=request.Form("txtdeptid")
duplicate=false

Set cn = Server.CreateObject("ADODB.Connection")
cn.Open(Conn)

Set rs = Server.CreateObject("ADODB.Recordset")
rs.open "department",cn,2,3

Do While Not (rs.eof OR duplicate)
If (StrComp(rs("deptName"),namedept,vbTextCompare)=0) OR (StrComp(rs("deptid"),iddept,vbTextCompare)=0)Then
duplicate=true
response.Redirect "deptsmaint.asp?error=1"
End If

rs.moveNext
Loop

If(duplicate = false) then
sql="INSERT INTO department (deptId, deptName, positionId) VALUES " & _
"( '" & Request.Form("txtdeptid") & "' , '" & Request.Form("txtdeptname") & "' ,'"& Request.Form("lstpos") & "' )"

cn.execute(sql)

Response.redirect "adddeptok.asp?success=1"

cn.close
set rs=nothing
set cn=nothing

End If

End Sub


Thks for helping me out urgently.

Gold pearl
 
The validation main function should be something like this. (+ is more or less like logical or to close to it)
[tt]
function checkWholeForm()
{
return textonly() && checkDropdown();
}
[/tt]
Then the submit button should take the return value.
[tt]
<input name="btnadd" type="submit" id="btnadd" value="Add" onclick="[red]return[/red] checkWholeForm()">
[/tt]
If anything still goes wrong, it is textonly or checkDropdown returning true when it should be false.
 
Thanks Gold pearl for the feedback and the vote(s). I appreciate it.
- tsuji
 
hmmm i think i rejoiced too quickly. the fact is that even when i do select an option form the drop down , i still get the error msg and the form still submits itself.

wat is the prob now?
 
Take an example of this function
[tt]
function checkDropdown()
{
if (frmdeptsmaint.lstpos.value == 0)
{
alert ("You didn't choose an option from the drop-down list.");
}
return false;
}
[/tt]
It's performance is that it will always return false even with an alert. It should be scripted like this.
[tt]
function checkDropdown()
{
if (frmdeptsmaint.lstpos.value == 0)
{
alert ("You didn't choose an option from the drop-down list.");
return false;
} else {
return false;
}
}
[/tt]
(Can be more concise, it is for illustration.)
Similarly for textonly function. It seems it suffers the same deficiency.
 
Further note: I did not look through what the validation process is to control, like frmdeptsmaint.lstpos.value. I take it for granted that those variables are correct until otherwise you tell us.
 
I'd a typo up there.
[tt]
else {
return [red]true[/red];
[/tt]
 
In textonly(),
>[tt]valid=false;[/tt]
replace it with
[tt] return false;[/tt]
I'm doing it piecemeal, you get the idea.
 
yes thks for that but let me specify that the prob is only with the dropdown list. even when i correctly select something and the deptname is ok, i still get the popup" you did not selected an option from drop down...
 
The corresponding segment of code seems wrong. Should it not be scripted at least like this?
[tt]
for(j = 0;j<checkOK.length;j++) [red]{[/red]
if( ch== checkOK.charAt(j)) break;
[red]}[/red]
if (j ==checkOK.length)
{
allValid = false
break;
}
if(ch!=",") [blue]//it would, would it not?[/blue]
allNum+=ch; [blue]//what for?[/blue]
[/tt]
 
hmm this bit can be removed and it will still work for the dept name. the problem lies with the dropdown and am having a really bad time finding out wat's wrong with it.
 
>the problem lies with the dropdown
Okay, if you look into the select-tag, here is something wrong.
[tt]
<option [red]value="<%=rspos(0)%>"[/red]><%=rspos(0)%></option>
[/tt]
But if you have corrected the validating function per my previous post:
[tt]
function checkDropdown()
{
if (frmdeptsmaint.lstpos.value == 0)
{
alert ("You didn't choose an option from the drop-down list.");
return false;
} else {
return true;
}
}
[/tt]
It would at least block all submission. (In any case, you are relying on ""==0 conversion. Should it make the conditional safer?)
 
well to be frank, i don't quite undertsand what u r trying to say..can u plz b a bit more explicit. thks
 
You have not set the value of options. You only set its innerHTML. The select tag will pick up nothing or empty string so to speak.
 
ah ok. ok i'll try this and let u know. thks for ur time.
 
Re-list your revised script. (Take out the irrelevant vbs part---don't know what it's for on the matter.) Better still is to list the client-side code from view-source, if it is not too big from the loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top