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!

Form Validation: Form Always Submits 1

Status
Not open for further replies.

Scorez2000

Programmer
Feb 5, 2004
87
GB
I'm tring to write some validation for a form, but even when I get a pop up saying that the data input is not as required, the form still tries to submit.

How do I stop this form submitting?


Here is the 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)
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>
 
Scorez2000,

I just revise it along the reading, try see what give.
Code:
<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
    [blue]Msgbox[/blue] "Please insert your name."    'Alert is uncommon, though will do. Why use undocumented feature for no advantage?
    tstFrm.tstNam.focus
    [blue]window.event.returnvalue=false[/blue]
    Exit Sub
  elseif isdate(tstFrm.tstDob.value) = false then
    [blue]Msgbox[/blue] "Please enter a valid date for your date of birth."    'same remark
    tstFrm.tstDob.focus
    [blue]window.event.returnvalue=false[/blue]
    Exit Sub
  elseif tstFrm.tstNat.value = "0" then
    [blue]Msgbox[/blue] "Please select a nationality."    'same remark
    requestleave.rlLast.focus
    [blue]window.event.returnvalue=false[/blue]
    Exit Sub
  End If
  
  btnSubmitClicked = MsgBox ("Are you sure you want to continue?", VBYesNo, "Please Confirm")
  If btnSubmitClicked = [blue]vbNo[/blue] then
    [blue]'Call tstFrm.Submit()    'I use the vbNo, hence changed
    window.event.returnvalue=false[/blue]
  end if
End Sub

</script>
The rest, the body seems okay at first sight. Any change would only be a matter of preference in style. I keep it unchanged.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top