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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can my form Validation be improved 3

Status
Not open for further replies.

Scorez2000

Programmer
Feb 5, 2004
87
GB
Hi all.

The method of form validation I use is client-side vbscript within my ASP application. Within the form, instead of having a submit button, I have a normal button. Then there's a vbscript that runs when the button is clicked.

This all runs fine and works exactly how I want except for one thing. Within a normal HTML form, the user can submit the form from any field by pressing enter. However, my code will not work unless the button is actually clicked.

Is there a way of making the form act like a normal web form (i.e. submit when the user presses enter).

Here is a sample of my code.
-----------------------------------------------------------
<html>
<head>
<script language="vbscript">

Sub btnSubmit_OnClick()
if requestleave.leavetype.value = "0" then
Alert "Please select the type of leave applied for."
Exit Sub
elseif isdate(requestleave.rlFirst.value) = false then
Alert "Please enter a valid date for the first day of leave."
requestleave.rlFirst.focus
Exit Sub
elseif isdate(requestleave.rlLast.value) = false then
Alert "Please enter a valid date for the last day of leave."
requestleave.rlLast.focus
Exit Sub
elseif cdate(requestleave.rlFirst.value) > cdate(requestleave.rlLast.value) then
Alert "The last day of leave must be after or the same as the first day of leave."
Exit Sub
elseif cdate(requestleave.rlFirst.value) = cdate(requestleave.rlLast.value) and requestleave.cblast.checked = True then
Alert "When booking a half day, only use the upper tickbox for selecting a half day"
Exit Sub
End If

if cdate(requestleave.rlFirst.value) <=date() then
btnSubmitClicked = MsgBox ("This leave request is in the past. Are you sure you want to continue?", VBYesNo, "Please Confirm")
If btnSubmitClicked = 6 then
Call requestleave.Submit()
end if
else
Call requestleave.Submit()
end if
End Sub

</script>
</head>
<body>
<h3>Leave Request</h3>
<table>
<form name="RequestLeave" id="RequestLeave" method="post" action="requestLeavedetails.asp">
<tr>
<td colspan="3"><b>Type Of Leave Applied for:</b></td>
<td colspan="4" align="right">
<select name="leavetype" id="leavetype">
<option value="0"></option>
<%
Set rsLeaveType = Conn.Execute("SELECT AbsenceType.AbsenceTypeID, AbsenceType.AbsenceType FROM AbsenceType WHERE (((AbsenceType.AbsenceType)=""Holiday"")) OR (((AbsenceType.AbsenceType)=""Special Leave"")) ORDER BY AbsenceType.AbsenceType;")
while rsLeaveType.EOF = "False"
Response.Write("<option value=""" & rsLeaveType("AbsenceTypeID") & """>" & rsLeaveType("AbsenceType") & "</option>")
rsLeaveType.movenext
wend
%>
</select>
</td>
</tr>
<tr>
<td colspan="6" align="center" style="font-size:xx-small">Use the tick boxes to mark the first or last day as half days. Then use the drop down to choose AM/PM for that day. If only one day is being taken, use the top box and dropdown.</td>
</tr>
<tr>
<td>Date:
<td><input id="rlFirst" name="rlFirst" size="7" maxlength="10"></td>
<td><input type="checkbox" id="cbFirst" name="cbFirst"></td>
<td>
<select name="ampmFirst" id="ampmFirst">
<option>AM</option>
<option>PM</option>
</select>
</td>
<tr>
<td>Date:
<td><input id="rlLast" name="rlLast" size="7" maxlength="10"></td>
<td><input type="checkbox" id="cbLast" name="cbLast">
<td>
<select name="ampmLast" id="ampmLast">
<option>AM</option>
<option>PM</option>
</select>
</td>
</tr>
<tr>
<td colspan="7" align="right"><input type="button" value="Next..." id="btnSubmit" name="btnSubmit"></td>
</tr>
</form>
</table>
</body>
</html>
 
try putting onSubmit=function in the form tag and make a real submit type button
 
yes i agree with bsl the "enter" issue will be resolved with the submit button
 
No, that doesn't work, because in that case the form will ALWAYS submit. I don't want the form to submit unless the data input into the form meets my criteria.
 
that's why onSubmit triggers your function then if conditions are right go to where you need to go....shouldn't be a problem...it's the same thing you have now except will work on the submit click AND a enter/return; while yours simply works on an onClick
 
You return False from the function if you want to cancel the submit.
 
I don't know how to "return false".

Here is some test code I am playing with. How do I change that so that if the conditions are not met, the form does not submit?
------------------------------------------------------

<html>
<head>
<title></title>
<style type="text/css">
body{font-family:verdana}
table{font-family:verdana;font-size:xx-small}
th{background-color:#999999}
</style>
<script language="vbscript">

'This script runs client-side (WD)
'This validates the info that the user inputs into the form, making sure that the info makes sense and the whole form is filled in (WD)
Sub Wibble()
if tstFrm.tstNam.value = "" then
Alert "Please insert your name."
tstFrm.tstNam.focus
Exit Sub
elseif isdate(tstFrm.tstDob.value) = false then
Alert "Please enter a valid date for your date of birth."
tstFrm.tstDob.focus
Exit Sub
elseif tstFrm.tstNat.value = "0" then
Alert "Please select a nationality."
requestleave.rlLast.focus
Exit Sub
End If

btnSubmitClicked = MsgBox ("Are you sure you want to continue?", VBYesNo, "Please Confirm")
If btnSubmitClicked = 6 then
Call tstFrm.Submit()
end if
End Sub

</script>

</head>
<body>
<p>This Is My Test Form.</p>
<table>
<form name="tstFrm" id="tstFrm" method="post" action="testpost.asp" onSubmit="Wibble">
<tr>
<th>Name</th>
<th>D.O.B.</th>
<th>Nationality</th>
</tr>
<tr>
<td><input name="tstNam" id="tstNam"></td>
<td><input name="tstDob" id="tstDob"></td>
<td><select name="tstNat" id="tstNat"><option value="0"></option><option value="1">British</option><option value="2">Irish</option></select></td>
</tr>
<tr>
<td colspan="3" align="right"><input type="submit" Value="Submit" name="tstSubmit" id="tstSubmit"></td>
</tr>
</form>
</table>
</body>
</html>
 
since you're using client-side validation use js; otherwise, most users won't see because it's vbs not js which is cross-browser support
 
Code:
<html>
<head>
<title></title>
<style type="text/css">
body{font-family:verdana}
table{font-family:verdana;font-size:xx-small}
th{background-color:#999999}
</style>
<script language="vbscript">

'This script runs client-side (WD)
'This validates the info that the user inputs into the form, making sure that the info makes sense and the whole form is filled in (WD)
Function Wibble()
  Wibble = False
  if tstFrm.tstNam.value = "" then
    Alert "Please insert your name."
    tstFrm.tstNam.focus
    Exit function
  elseif isdate(tstFrm.tstDob.value) = false then
    Alert "Please enter a valid date for your date of birth."
    tstFrm.tstDob.focus
    Exit function
  elseif tstFrm.tstNat.value = "0" then
    Alert "Please select a nationality."
    requestleave.rlLast.focus
    Exit function
  End If
  
  btnSubmitClicked = MsgBox ("Are you sure you want to continue?", VBYesNo, "Please Confirm")
  If btnSubmitClicked = 6 then
    Wibble = True
  end if
End function

</script>

</head>
<body>
<p>This Is My Test Form.</p>
<table>
<form name="tstFrm" id="tstFrm" method="post" action="testpost.asp" onSubmit="jscript: return Wibble();">
<tr>
  <th>Name</th>
  <th>D.O.B.</th>
  <th>Nationality</th>
</tr>
<tr>
  <td><input name="tstNam" id="tstNam"></td>
  <td><input name="tstDob" id="tstDob"></td>
  <td><select name="tstNat" id="tstNat"><option value="0"></option><option value="1">British</option><option value="2">Irish</option></select></td>
</tr>
<tr>
  <td colspan="3" align="right"><input type="submit" Value="Submit" name="tstSubmit" id="tstSubmit"></td>
</tr>
</form>
</table>
</body>
</html>
 
The above uses JavaScript calling notation.

Below is pure VBScript:
Code:
<html>
<head>
<title></title>
<style type="text/css">
body{font-family:verdana}
table{font-family:verdana;font-size:xx-small}
th{background-color:#999999}
</style>
<script language="vbscript">

'This script runs client-side (WD)
'This validates the info that the user inputs into the form, making sure that the info makes sense and the whole form is filled in (WD)
Function tstFrm_onSubmit()
  tstFrm_onSubmit = False
  if tstFrm.tstNam.value = "" then
    Alert "Please insert your name."
    tstFrm.tstNam.focus
    Exit function
  elseif isdate(tstFrm.tstDob.value) = false then
    Alert "Please enter a valid date for your date of birth."
    tstFrm.tstDob.focus
    Exit function
  elseif tstFrm.tstNat.value = "0" then
    Alert "Please select a nationality."
    requestleave.rlLast.focus
    Exit function
  End If
  
  btnSubmitClicked = MsgBox ("Are you sure you want to continue?", VBYesNo, "Please Confirm")
  If btnSubmitClicked = 6 then
    tstFrm_onSubmit = True
  end if
End function

</script>

</head>
<body>
<p>This Is My Test Form.</p>
<table>
<form name="tstFrm" id="tstFrm" method="post" action="testpost.asp">
<tr>
  <th>Name</th>
  <th>D.O.B.</th>
  <th>Nationality</th>
</tr>
<tr>
  <td><input name="tstNam" id="tstNam"></td>
  <td><input name="tstDob" id="tstDob"></td>
  <td><select name="tstNat" id="tstNat"><option value="0"></option><option value="1">British</option><option value="2">Irish</option></select></td>
</tr>
<tr>
  <td colspan="3" align="right"><input type="submit" Value="Submit" name="tstSubmit" id="tstSubmit"></td>
</tr>
</form>
</table>
</body>
</html>

It is best to use JavaScript for client side code. Even in a controlled invironment it wont be long before some wiseguy decides to download FireFox...
 
Ok, that's it working perfectly now.

Any idea why Firefox won't support this? I really don't want to have to learn a whole new language just to use this in other applications.
 
VBScript is a microsoft thing.

JavaScript (aka JScript... aka ECMA Script) is a standard.
 
i thought you said it was a controlled ie env...;hence , is why i said js is the beter option...because vbs is MS---ie
 
Yes, the code I am planning to use is for a controlled environment, but I do other work outisde of that and i f I wanted to do the same type of thing, I would have to learn javascript.
 
it's that much different except the curlies..same logic...i'll check for a form validatot example....lol..but i'm sure sheco or steven will beat me too the punch......but seriously, js is the way to go...once you have a template the logical flow is easy to follow...checking
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top