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>
 
sheco? is the alert bos "pure" vbs...not being a sa...just wondering if msgBox is the purer
 
huh? sorry I was in a meeting for the last hour or so, did I miss something?
 
scorez,

as you can see it's not much diferent than vbs...see if this works in mozilla/firefox....did on-the-fly...so sheco...if you see a typo/error..please point out..thanks

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="javascript">

function ValidateForm(form)
{
   if(document.tstFrm.tstNam.value == "" )
   { 
      alert('You have not entered a name') 
      document.tstFrm.tstNam.focus(); 
      return false; 
   } 

if(document.tstFrm.tstDob.value == "" )
   { 
      alert('You have not entered a DOB') 
      document.tstFrm.tstDob.focus(); 
      return false; 
}

if(document.tstFrm.tstNat.selectedIndex == "" )
   { 
      alert('You have not entered a nationality') 
      document.tstFrm.tstNat.focus(); 
      return false; 
   } 
   
 
 
 
return true;
 
} 

</script>

</head>
<body>
<p>This Is My Test Form.</p>
<table>
<form name="tstFrm" id="tstFrm" method="post" action="testpost.asp" onsubmit="javascript:return ValidateForm()">
<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" onClick="CheckForm();"></td>
</tr>
</form>
</table>
</body>
</html>
 
BSL, I'll give you a star because I think you deserve it after all that ! ;-)

Just a small additional comment:

As it is really helpful if the user is told about all of their errors in one go - rather than correcting one mistake at a time in the form, you could use something like this:

Code:
function validateForm()
{
  var iErrCount = 0;
  var sErrText = '';

  if(document.tstFrm.tstNat.selectedIndex == "" )
  {
    //First increment the error counter so that we know there is an error.
    iErrCount++;
    //Then write some meaningful text
    sErrText += '\n' + iErrCount + '. You have not entered a nationality');
    //Then set the form input box background to red to show which field is in error... 
    //You could make this more aesthetically pleasing and just do the border, or the field name text or whatever takes your fancy.
    document.tstFrm.tstNat.style.background = '#ff0000';
  } 

   if(document.tstFrm.tstNam.value == "" )
   {
     iErrCount++;
     sErrText += '\n' + iErrCount + '. You have not entered a name');
    document.tstFrm.tstNam.style.background = '#ff0000';
   }

   if(document.tstFrm.tstDob.value == "" )
   {
      iErrCount++;
      sErrText += '\n' + iErrCount + '. You have not entered a DOB');
      document.tstFrm.tstFrm.style.background = '#ff0000';
   }

   if (iErrCount > 0 ) 
   {
      alert('There were ' + iErrCount + ' errors with the data you submitted:\n\n' + sErrText + '\n');
      return false;
   } else {
      if (confirm('Are you sure you want to submit this form?'))
      {
           return true;
      } else {
           return false;
      }
   }
}

I would also suggest that you build yourself a standard set of functions in whatever language you decide upon to validate certain conditions, then call upon this library when processing a form. Saves a lot of time and keeps your code clean.

Also, validate it again on the server - just because you used your web page to post the data to the server doesn't mean nasty hackers will be so thoughtful. I validate on both - on the client side for user experience and on the server side just to be safe.

Good luck.. !

Damian

A smile is worth a thousand kind words. So smile, it's easy! :)
 
sweeeet......dang another one in the goodie bag damber....keep them coming! ;-) btw thanks for the star....i was teasing sheco because he types faster than me..lol...anyways i def can use this!! so one on the way and thanks for the advice

Brian
 
I would do it in pure ASP, then no matter what it works even if JS is turned off.

Site San Diego www.sitesd.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top