Validation for VBScript Web Form

I am having some problems adding validation to my VBScript Web Form.  I created the form a couple months ago and now I need to add validation to ensure that certain boxes are filled in.

I have researched this issue and believe that I have a resolution but when I try to add the coding into my already existing script it errors out.

I have attached my code for review.  If possible can someone show me how to make the matreq.EmployeeName.value field be validated for text entered?

Thanks in advance!
<html>
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Materials Request Form</title>
<script language=VBScript>
<!--
Option Explicit
 
Dim NewXlsFile, XLSTemplate
 
Sub SaveData()
Dim DirectoryName, EmpName, TodaysDateString, FullFileName
 
DirectoryName = "G:\Share\Corp\Web Forms\MaterialsRequestForm\"
EmpName = Replace(matreq.EmployeeName.Value," ","_",1) 'Replace spaces with underscore
TodaysDateString = Replace(matreq.TodaysDate.Value,"/","_",1) 'Replace slash with underscore
TodaysDateString = Replace(TodaysDateString,".","_",1) 'Replace slash with underscore
 
FullFileName = TodaysDateString & "_" & EmpName & ".xls"
 
XLSTemplate = DirectoryName & "DONOTDELETE.xls"
NewXlsFile = DirectoryName & FullFileName
 
SaveToExcel XLSTemplate, NewXlsFile
 
MsgBox "Thank you " & matreq.EmployeeName.value & " your request has been submitted on " & matreq.TodaysDate.value & " to the appropriate department."
End Sub
 
Private Sub SaveToExcel(XLSTemplate, NewXlsFile)  ' Removed the object names from here.
Dim oWs, oWb
Dim objExcel
Dim objWorkbook
 
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(XLSTemplate) 'open the template workbook
'objExcel.Visible = True
objExcel.Visible = False 'This will cause the Excel application to not show
Set oWs = objExcel.ActiveSheet
Set oWb = objExcel.ActiveWorkbook
 
'First number is the row, second number is the column
'first column on template
oWS.Cells(7, 1).Value = matreq.EmployeeName.Value 'row 1, column 1
oWS.Cells(9, 1).Value = matreq.CompanyName.Value 'row 2, column 1
oWS.Cells(11, 1).Value = matreq.LE_InvestorRelationsManagerName.Value
oWS.Cells(13, 1).Value = matreq.TodaysDate.Value
oWS.Cells(15, 1).Value = matreq.DateAndTimeNeeded.value
oWS.Cells(17, 1).Value = matreq.NumberOf2006_2007Donors.value
oWS.Cells(19, 1).Value = matreq.CurrentNumberOfEmployees.value
oWS.Cells(23, 1).Value = matreq.MatReqNotes.value
oWS.Cells(28, 2).Value = matreq.LeadershipBrochures.value
oWS.Cells(33, 1).Value = matreq.Notes.value
'second column on template
oWS.Cells(9, 5).Value = matreq.PledgeOC.value
oWS.Cells(10, 5).Value = matreq.PledgeGeneric.value
oWS.Cells(11, 5).Value = matreq.PledgeSpanish.value
oWS.Cells(14, 5).Value = matreq.CampaignEnglish.value
oWS.Cells(15, 5).Value = matreq.CampaignSpanish.value
oWS.Cells(18, 5).Value = matreq.BookmarksOCUW_211.value
oWS.Cells(19, 5).Value = matreq.BookmarksVolunteerSolutions.value
oWS.Cells(20, 5).Value = matreq.BookmarksBottomLine.value
oWS.Cells(23, 5).Value = matreq.PostersTwoSidedEnglish_Spanish.value
oWS.Cells(24, 5).Value = matreq.PostersVolunteerSolutions.value
oWS.Cells(26, 5).Value = matreq.PostersThermometers.value
oWS.Cells(29, 5).Value = matreq.EMCPacketsWithSpanishBrochure.value
oWS.Cells(30, 5).Value = matreq.ECMPacketsWithoutSpanishBrochure.value
oWS.Cells(32, 5).Value = matreq.ECMPacketsNewHireEN.value
oWS.Cells(33, 5).Value = matreq.ECMPacketsNewHireSP.value
oWS.Cells(34, 5).Value = matreq.ECMPacketsUWBaloons.value
oWS.Cells(36, 5).Value = matreq.ECMPackets0708CampCatalog.value
'third column on template
oWS.Cells(9, 9).Value = matreq.VideosDateFrom.value
oWS.Cells(10, 9).Value = matreq.VideosDateTo.value
oWS.Cells(11, 9).Value = matreq.VideosComImpactVHS.value
oWS.Cells(12, 9).Value = matreq.VideosComImpactDVD.value
oWS.Cells(14, 9).Value = matreq.VideosComImpactLoop_VHS.value
oWS.Cells(15, 9).Value = matreq.VideosComImpactLoop_DVD.value
oWS.Cells(19, 9).Value = matreq.BannersDateFrom.value
oWS.Cells(20, 9).Value = matreq.BannersDateTo.value
oWS.Cells(21, 9).Value = matreq.BannersPodium.value
oWS.Cells(22, 9).Value = matreq.BannersOCUWEvent3x12.value
oWS.Cells(23, 9).Value = matreq.BannersOCUWEvents2x6.value
oWS.Cells(24, 9).Value = matreq.BannersCiyBanners.value
 
objExcel.DisplayAlerts = False 'Turn alerts off is saving the file as a file that already exists.
 'Turning off alerts will stop the message "The file already exists. Do you want to save?"
 
oWs.SaveAs(NewXlsFile)
oWb.Close
objExcel.Quit
'
Set oWs = Nothing
Set oWb = Nothing
Set objExcel = Nothing
 
End Sub
-->
</script>

Open in new window

OCUWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, this may be a little messy, but where you intend to do something with the data, first make a call to this Validate_Data function:

Function Validate_Data
      boolValid = True
      If Trim(matreq.EmployeeName.Value) = "" Then
            MsgBox "Please enter an Employee name."
            boolValid = False
            matreq.EmployeeName.SetFocus
      End If
      If Trim(matreq.CompanyName.Value) = "" Then
            MsgBox "Please enter a Company name."
            boolValid = False
            matreq.CompanyName.SetFocus
      End If
      If Trim(matreq.LE_InvestorRelationsManagerName.Value) = "" Then
            MsgBox "Please enter a Manager name."
            boolValid = False
            matreq.LE_InvestorRelationsManagerName.SetFocus
      End If
      If Trim(matreq.TodaysDate.Value) = "" Then
            MsgBox "Please enter today's date."
            boolValid = False
            matreq.TodaysDate.SetFocus
      End If
      If Trim(matreq.DateAndTimeNeeded.value) = "" Then
            MsgBox "Please enter a date and time."
            boolValid = False
            matreq.DateAndTimeNeeded.SetFocus
      End If
      If Trim(matreq.NumberOf2006_2007Donors.value) = "" Then
            MsgBox "Please enter the number of donors."
            boolValid = False
            matreq.NumberOf2006_2007Donors.SetFocus
      End If
      If Trim(matreq.CurrentNumberOfEmployees.value) = "" Then
            MsgBox "Please enter the number of employees."
            boolValid = False
            matreq.CurrentNumberOfEmployees.SetFocus
      End If
      If Trim(matreq.MatReqNotes.value) = "" Then
            MsgBox "Please enter required notes."
            boolValid = False
            matreq.MatReqNotes.SetFocus
      End If
      If Trim(matreq.LeadershipBrochures.value) = "" Then
            MsgBox "Please enter the leadership brochures."
            boolValid = False
            matreq.LeadershipBrochures.SetFocus
      End If
      If Trim(matreq.Notes.value) = "" Then
            MsgBox "Please enter Notes."
            boolValid = False
            matreq.Notes.SetFocus
      End If

      Validate_Data = boolValid
End Function


and use the results from that to determine whether or not to use the data.

Sub SaveData()
   If Validate_Data = True Then
      ' do stuff like savind the data
   End If
End Sub



Regards,

Rob.
0
OCUWAuthor Commented:
Ok, so as it stands I have it set up with the code I already attached, and this is the other important part I guess I left out.

<tr>
      <input id=cmdProcess onclick="SaveData" type=button value=Submit name="Submit">
</tr>

Based on this, I am referencing the Save Data Sub.  I can easily change that to the Validate Data Sub, but what coding would be used for, "If all data validates, go to SaveData(), else MsgBox"?

Thanks for the help!
0
RobSampsonCommented:
You shouldn't need to change which function your button calls.  With the smaller snippet of code I posted:

Sub SaveData()
   If Validate_Data = True Then
      ' do stuff like savind the data
   End If
End Sub

... the existing code in your SaveData sub would just go where the "' do stuff'" is.

That way, the button calls SaveData, that Sub calls Validate_Data, and gets true or false back.  That should do it I think.

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

OCUWAuthor Commented:
Great, so just to fully clarify... my code would look something like this.

Function ValidateData()
      boolValid = True
      If Trim(matreq.EmployeeName.Value) = "" Then
            MsgBox "Please enter an Employee name."
            boolValid = False
            matreq.EmployeeName.SetFocus
      End If
...

Sub SaveData()
      If ValidateData = True Then
            Dim DirectoryName, EmpName, TodaysDateString, FullFileName
 
            DirectoryName = "G:\Share\Corp\Web Forms\MaterialsRequestForm\"
            EmpName = Replace(matreq.EmployeeName.Value," ","_",1) 'Replace spaces with underscore
            TodaysDateString = Replace(matreq.TodaysDate.Value,"/","_",1) 'Replace slash with underscore
            ...
      End If
End Sub

And from there I can just call the SaveData.  I think I got it here, I will test it out today.  Thanks!
0
OCUWAuthor Commented:
Ok, so with your help I was able to make this work, but I am getting an error.  Assuming I do NOT enter the employee name and click submit, the MsgBox pops up as it should, but after this happens I get the following error.

Object doesn't support this property or method:
'matreq.EmployeeName.SetFocus'

Is there another command I could use instead of the SetFocus?  What exactly is it doing?  Thanks again, almost there!
0
OCUWAuthor Commented:
Never mind, I just disabled the SetFocus option.  After researching it I figure its not needed.  Everything is working now.

If you have a fix for this or know why its not working please let me know.  Otherwise I am closing this out.  Thank you VERY much for all the help and have a great day!
0
RobSampsonCommented:
You're right, it's not specifically required, but it just aids in usability I guess. I like to give the invalid control the "focus" so the user knows which field needs changing.

Instead of
matreq.EmployeeName.SetFocus

you can try
matreq.EmployeeName.Focus

You just need to find the right command to give a control the focus on your web form....

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.