View Full Version : stop a form being posted

02-12-2003, 07:14 PM
hi again guys, thanks for the last help, i have taken note of the login weaknesses pointed out by whammy and glenngv and changed that code, though i did say i wanted to learn how to do those things for other purposes and that has now been successfully completed...i have also tidied up my code, removing dozens of superflous lines and unneccessary variables littered around the place...now my problem is with this function to ensure certain required fields are entered to text boxes on an add new form

Function CkISfilled()
strvenue = Document.Form.Tvenue.Value
strevent = Document.Form.Tevent.Value

IF Document.Form.Tvenue.Value="" OR Document.Form.Tevent.Value="" THEN
msgBox "You must enter required Fields"


this function runs then the form is posted so what i what to do is stop the form being posted and after this function, can this be done?.

02-12-2003, 07:20 PM
/me doesnt just a do® this kinda stuff...
'cept ummm...did ya try telling it to just a lert® vs the msgBox ???

just a guessin'® n' keeping ya company til the pros come just a long® lol...

02-12-2003, 07:48 PM
If this function is returning the validation then you need to return a boolean of whether the form is valid or not.

Function CkISfilled(strvenue,strevent)
IF strvenue="" OR strevent="" THEN
CkISfilled = false
msgBox "You must enter required Fields"
CkISfilled = true

If CkISfilled(Document.Form.Tvenue.Value, Document.Form.Tevent.Value ) Then
'It is true so do form processing
'It is false we do not want to process
End If

I just realized you are using vbscript. I am not sure if my syntax is correct but the conept is.

02-13-2003, 02:09 AM
Pretty close to what I'd do, but I wouldn't use VBScript client-side... what I would do:

<% @Language="VBScript" %>
<% Option Explicit %>
Dim Tvenue
Dim Tevent
Dim NumberOfTimesSubmitted

Tvenue = Trim(Request.Form("Tvenue"))
Tevent = Trim(Request.Form("Tevent"))
NumberOfTimesSubmitted = Request.Form("NumberOfTimesSubmitted")

'************************************ MAIN PROGRAM

NumberOfTimesSubmitted = NumberOfTimesSubmitted + 1

If Len(Tvenue) = 0 OR Len(Tevent) = 0 Then
Call DisplayForm()
Call ProcessInformation()
End If

'******************************** END MAIN PROGRAM

'************************************* SUBROUTINES

Sub ProcessInformation()
'Do whatever you want to do here...
End Sub()

<% Sub DisplayForm() %>
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
This would be your form with your input boxes with the appropriate names!<br /><br />

You'd also want to add a hidden field, called "NumberOfTimesSubmitted" before your submit button...<br /><br />

Then, if <br /><br />

NumberOfTimesSubmitted > 1<br /><br />

When you redisplay your form using the subroutine "DisplayForm()", show the user an error!


02-13-2003, 01:38 PM
thanks guys, i will try and get this to work, though it may take a few days....
....well just a few hours instead and it works a dream, thanks again whammy, fantastic! I had to give the hidden field NumberOfTimesSubmitted an initial value of 0 to get it to work as it kept giving a string: "" error. But there is no incrementing counter or anything on NumberOfTimesSubmitted so i have yet t o figure out how to display an error message.

02-14-2003, 12:50 AM
The incrementing counter is this:

NumberOfTimesSubmitted = NumberOfTimesSubmitted + 1

What you need to do is have a hidden field like this, right before your submit button:

<input type="hidden" name="NumberOfTimesSubmitted" value="<% = NumberOfTimesSubmitted %>" />

Then, it will pass it from page to page.

To show an error, you can just do something like:

<% If NumberOfTimesSubmitted > 1 AND Len(yourfield) = 0 Then %>
<span style="color:#ff0000> *Required</span>
<% End If %>

Obviously there are a number of ways to do this, this is one simple method.

02-14-2003, 02:08 PM
Thanks whammy i got it to work doing something similar ^, im off to think through the logic for my next problem now, which should be interesting. i have entered the date of an event in the database like this, day, date, month fields, as text eg. MON, 23rd, jun. But I now want to display data as ORDER by the date fields with the soonest being displayed first. Kind of impossible HUH! So i plan to write a function that will hopefully replace the date, month fields with a date that can be added to a date field like 23-06-03 in the database to order by when displaying data. I think it is possible to do this, but if it is a bad idea for any reason could you advise?

02-15-2003, 12:34 AM
Actually, that sounds quite easy to me...

You should use a DateTime field in Access or a shortdatetime or datetime field in SQL Server to store dates though, usually (instead of a text field), at least in my opinion, some people may differ in their opinion... however, for me that makes it very easy to sort by date or select a date range using a SQL statement later on using things like DATEDIFF() or DATEPART() right in the SQL Statement itself.

As a matter of fact, I might look up DatePart() and DateDiff() on MSDN and w3schools.com if I were you. It definitely can't hurt to know this stuff...

Also, you can probably write a function that pulls each "text" date in your database, splits it up (probably by comma since you're using that by a delimiter), and then creates a new "real" date from that information. Check out the CDate() function. Then you can update a new datetime field in your database for each record with the "true" date returned returned from your function (after you make sure that your function is really returning a valid date, of course!)... which should allow you get the kind of results you want much easier.

Once you've updated all of the fields, and updated your scripts to NOT write to that text field anymore, I'd delete that field entirely, since a text date field is pretty much useless in many cases...

P. S. In the future, you may also want to check out this code, it may help you get a quicker start when selecting date ranges in the first place:


Not to mention it's a pretty compact way to display date dropdowns in classic ASP IMHO... and it also validates that a valid date was entered on the client side.

If you do end up using it and need help validating the date on the server-side as well, it's not too hard. To validate the dates, you just call the IsDate() function of VBScript. Once you're sure they are both valid dates you can run a DateDiff() comparison against them or whatever... the reason you want to use IsDate() first is because DateDiff() will throw an error if you don't input valid dates into it.

02-15-2003, 06:35 PM
It would have been easier to put a proper date field in at the start or go back and do it now, but now i have got too pigheaded to go back and change things, especially as every small change i make seems to have a seriously detrimental effect on all of my code....anyway it was for the fun and practice as well as the stubbornness, this is how i did it anyway, it works fine enuff even if it is really uneccessary and resource consuming code, but it took me a whole day to figure this little bit out, only problem is figuring out a reliable way to do the year as it means a lot of problems in the future..maybe just add a 'year' dropdown list to the form or something...

Dim strmonth
Dim strdate
Dim stryear

strdate = Trim(Request.Form("Tdate"))
strmonth = Trim(Request.Form("Tmonth"))
stryear = "03"

' format month, date and year for insert into DB formatdate field
'from text boxes with 23rd & june, then add stryear and the result = 23-jun-03

'month first
IF Len(strmonth) > 0 THEN
strmonth = "-" & Left(strmonth, 3) & "-"
strmonth = somethingelsecompletelydifferent

'date next
IF Len(strdate) > 3 THEN
strdate = Left(strdate, 2)
ELSEIF Len(strdate) <> 0 THEN
strdate = 0 & Left(strdate, 1)
strdate = ""

a sign of improvement in my efforts though, even though it took all day to get this far, my past problems were in inserting into database but somehow i suceeded first time by just going

rstpubdata("Formatdate") = strdate & strmonth & stryear