Click to See Complete Forum and Search --> : Non-functioning validation


larry101
September 14th, 2004, 11:17 AM
Hello one & all ,
I am relatively new to scripting. I am working on an application which involves asp and ms access2000. It writes to and reads from the database. The clientside validation i have implemented are not correctly working - a common problem is that form submission occurs irrespective of errors.In this example the new and confirmnew passwords are compared and even though they are not the same, the new password is submitted to the database. What is wrong with the validation code. Any help would be greatly appreciated. Below is an example of code.Thanks in advance,
larry101


CHANGE PASSWORD PAGE CODE

<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">

Function TextBoxValid(textbox)
Dim textlen
TextBoxValid = True
textlen = Len(textbox)

If textlen <> 8 Then
TextBoxValid = False
End If
End Function

Function checkInput

If TextBoxValid(ChangePass.NewPassword.value)= False Then
checkInput = False
MsgBox "The required number of characters for a password is 8",0,"ERROR"
Exit Function
End If

If TextBoxValid(ChangePass.ConfirmNewPassword.value)= False Then
checkInput = False
MsgBox "The required number of characters for password confirmation is 8",0,"ERROR"
Exit Function
End If

If ((ChangePass.NewPassword.value) <> (ChangePass.ConfirmNewPassword.value)) Then
checkInput = False
MsgBox "The new password you entered does not match the confirm password.
Please re-enter",0,"ERROR"
Exit Function
End If

End Function

Function formValid()
If checkInput = True Then
formValid = True
Else
formValid = False
End If
End Function

</SCRIPT>

<TITLE>Update User Password</TITLE>
</HEAD>






<BODY>
<FORM NAME="ChangePass" ACTION="UpdatePassTable.asp" METHOD="POST" onSubmit="formValid()">
Old Password :&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<INPUT TYPE="Password" NAME="OldPassword" SIZE="8" MAXLENGTH="8">
<BR>
<BR>
New Password :&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<INPUT TYPE="Password" NAME="NewPassword" SIZE="8" MAXLENGTH="8">
<BR>
Confirm New Password :
<INPUT TYPE="Password" NAME="ConfirmNewPassword" SIZE="8" MAXLENGTH="8">
<BR>
<BR>
<INPUT TYPE="Submit" VALUE="Submit">
&nbsp
&nbsp
<INPUT TYPE="Reset" VALUE="Reset">

<BR>
</TABLE>
</FORM>
</BODY>
</HTML>



UpdatePassTable.asp CODE – checks usertype and updates the associated table

<% @LANGUAGE=VBScript %>

<!--#include file="adovbs.inc" -->


<%

Dim objConn, query, objRS

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "DSN=Profiles&Placements"

If (Session("usertype") =0) Then

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "TPCLogin", objConn, adOpenDynamic, adLockOptimistic

While Not objRS.EOF
If (objRS("username") = Session("username")) Then
objRS.Delete
End If
objRS.MoveNext
Wend
objRS.Close

query = "SELECT * FROM [TPCLogin]"

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open query, objConn, adOpenDynamic, adLockOptimistic

objRS.AddNew
objRS("username")= Session("username")
objRS("password")= Request.Form("NewPassword")
objRS.Update

Else

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "StudLogin", objConn, adOpenDynamic, adLockOptimistic

While Not objRS.EOF
If (objRS("username") = Session("username")) Then
objRS.Delete
End If
objRS.MoveNext
Wend
objRS.Close

query = "SELECT * FROM [StudLogin]"

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open query, objConn, adOpenDynamic, adLockOptimistic

objRS.AddNew
objRS("username")= Session("username")
objRS("password")= Request.Form("NewPassword")
objRS.Update


End If


objRS.Close
Set objRs = Nothing

objConn.Close
Set objConn = Nothing

Response.Redirect "UserTypeCheck.asp"

%>


Problem : error messages are showing but form is submitted anyway. Also if NewPassword is
different from ConfirmNewPassword , the string/value of NewPassword is submitted
and entered into the database irrespective.

Vanny
September 20th, 2004, 03:29 AM
When using onSubmit in the <FORM> tag i think you need

onSubmit="return formValid()">

and pass back whether true or false from the function, true will submit, false will cancel. This is how it works with Javscript validation.

I would recomend JS validation in pages as not all browser support client side VBSCRIPT. But javascript is a standard.

I actually, dont call my validation from the form tag anymore, i prefer to call a function from the submit button. This removes the user accidently pressing enter instead of tab and submitting the form.

You would need to change your button type though from

<INPUT TYPE="Submit" VALUE="Submit">

to

<INPUT TYPE=button NAME="btnSubmit" VALUE="Submit" onClick="formValid()">

Then in your form valid function if all correct, do a document.changePass.submit() or whatever the VBSCRIPT submit function is.