Skip Navigation

Random Questions - Validation

Related Pages

MVP Logo

Some pages below may open in a new window or tab Links open in a new window or tab

The code for the Random Questions page - this is written in VB Script for "classic" ASP.

<%
'Set the Random Question
Dim rq(3),rqa(3)
rq(1)="What colour is a blue sky?"
rq(2)="What is the fourth word in this question?"
rq(3)="How many days are there in a week?"
rqa(1)="blue"
rqa(2)="fourth"
rqa(3)="7"
%>


<%
eTo = "user@example.com"
eBody = ""

'Did the user click the "Send" button?
if Request.Form("formID") = "00" then
  'Get Form or QueryString parms
  rfcheck = validHTML(Request.Form("randquest"))
  rfcheck = lcase(rfcheck)
  rr = cint(request.form("ranq"))
  rfComments = validHTML(Request.Form("Comments"))
  eName   = validHTML(Request.Form("Name"))
  eFrom   = validHTML(Request.Form("Replyto"))
  eSubject = validHTML(Request.Form("Subject"))

  'Do some checks
  'allow verbose answer for days in week (change word to numeric)
  if rfcheck="seven" then rfcheck="7" end if
  'Compare user's answer with correct answer
  if rfcheck<>rqa(rr) then 
    arrayErrors = arrayErrors & "|checks"
  end if
  if len(eName) = 0 then
    arrayErrors = arrayErrors & "|eName"
  end if
  if len(eFrom) = 0 then
    arrayErrors = arrayErrors & "|eFrom"
  else
    jj = inStr(eFrom,"@")
    if jj = 0 or inStr(mid(eFrom,jj),".") = 0 then
      arrayErrors = arrayErrors & "|eFrom"
    end if
    if invalidChar(eFrom,1,"@.-_") then
      arrayErrors = arrayErrors & "|eFrom"
    end if
  end if
  if len(eSubject) = 0 then
    arrayErrors = arrayErrors & "|eSubject"
  end if
  if len(rfComments) = 0 then
    arrayErrors = arrayErrors & "|Comments"
  end if

  'If there are no errors, send the email.
  if len(trim(arrayErrors)) = 0 then
    'build eBody
    eBody = eBody & rfComments & vbCRLF & vbCRLF
    eBody = eBody & "Name: " & validHTML(Request.Form("Name")) & vbCRLF
    eBody = eBody & "email: " & eFrom & vbCRLF & vbCRLF
    eBody = eBody & "IP Address: " & svrh & vbCRLF
    eBody = eBody & "Time Posted: " & svts & vbCRLF
  end if
else arrayErrors = "|nofields"
end if

Back to Random Questions