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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ClientSide VBScript not running 3

Status
Not open for further replies.

cruford

Programmer
Dec 6, 2002
138
US
Can anyone tell me why this is not working. None of the msgbox's come up so the entire function isn't firing with the forms onSubmit. I have this script in a few other pages and it works fine.

Code:
<script language="vbscript">
	Function frm_onSubmit()
		Dim sMsg, nErr
		'MsgBox's are for testing
		If document.frmEmpInfo.txtFN.Value = "" Then
			sMsg = sMsg & "First name is required." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If document.frmEmpInfo.txtLN.Value = "" Then
			sMsg = sMsg & "Last name is required." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If document.frmEmpInfo.cboDept.Value = "" Then
			sMsg = sMsg & "Department is required." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If document.frmEmpInfo.txtTitle.Value = "" Then
			sMsg = sMsg & "Title is required." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If document.frmEmpInfo.txtExt.Value = "" Then
			sMsg = sMsg & "Extension is required." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If len(document.frmEmpInfo.txtExt.Value <> 4 Then
			sMsg = sMsg & "Extension must be 4 digits." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If Not isnumeric(document.frmEmpInfo.txtTitle.Value) Then
			sMsg = sMsg & "Extension must be a number." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If document.frmEmpInfo.cboCube.Value = "" Then
			sMsg = sMsg & "Cube is required." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		If len(document.frmEmpInfo.txtUserId.Value) > 8 Then
			sMsg = sMsg & "User ID's must be 8 or less characters." & VbCrLf
			MsgBox sMsg
			nErr = 1
		End If
		Select Case nErr 
			Case 1
				MsgBox sMsg,vbOKOnly,"Incorrect Information"
				window.event.returnValue = False
			Case Else
				MsgBox "No errors found"
				window.event.returnValue = True
		End Select
	End Function
</script>

And here is the select case that builds that form
Code:
<%Select Case request.querystring("CMD")
		Case "Add"%>
			<br>
			<form method="get" action="Emp_Information.asp" name="frmEmpInfo" onsubmit="frm_onSubmit();">
			<table width="100%" style="background: navy; color: white; font-size: 10pt; font-weight: bold">
				<tr><td align="center"><font color="white">Add Employee</font></td></tr>
			</table>
			<table class="TDBody" border="4" cellpadding="1" cellspacing="1" align="center">
				<tr><td align="right">First Name:&nbsp;</td><td><input type="text" maxlength="25" size="25" name="txtFN"></td></tr>
				<tr><td align="right">Last Name:&nbsp;</td><td><input type="text" maxlength="25" size="25" name="txtLN"></td></tr>
				<tr><td align="right">Department:&nbsp;</td><td><select name="cboDept">
					<option value=""></option>
				<%sSQL = "SELECT * FROM ztblDept ORDER BY Description"
					Set RS = Conn.Execute(sSQL,,1)
					Do While Not RS.EOF%>
						<option value="<%=RS.Fields("DeptCode")%>"><%=RS.Fields("Description")%></option>
						<%RS.MoveNext
					Loop
					RS.Close%>
					</select></td></tr>
				<tr><td align="right">Title:&nbsp;</td><td><input type="text" maxlength="50" size="25" name="txtTitle"></td></tr>
				<tr><td align="right">Extension:&nbsp;</td><td><input type="text" maxlength="4" size="4" name="txtExt"></td></tr>
				<tr><td align="right">Cube:&nbsp;</td><td><select name="cboCube">
					<option value=""SELECTED></option>
					<%sSQL = "SELECT * FROM ztblCubes"
					Set RS = Conn.Execute(sSQL,,1)
					Do While Not RS.EOF%>
						<option value="<%=RS.Fields("CubeNumber")%>"><%=RS.Fields("CubeNumber")%></option>
						<%RS.MoveNext
					Loop
					RS.Close%>
					</select></td></tr>
				<tr><td align="right">User ID:&nbsp;</td><td><input type="text" maxlength="8" size="8" name="txtUserID"></td></tr>
				<tr><td align="right">Pic Name:&nbsp;</td><td><input type="text" maxlength="15" size="15" name="txtPicName"></td></tr>
					<tr><td align="right">Manager:&nbsp;</td><td><select name="cboManager">
							<option value="0"SELECTED>NO</option>
							<option value="1">YES</option>
						</select></td></tr>
					<tr><td align="right">Supervisor:&nbsp;</td><td><select name="cboSupervisor">
							<option value="0"SELECTED>NO</option>
							<option value="1">YES</option>
						</select></td></tr>
					<tr><td align="right">New Hire:&nbsp;</td><td><select name="cboNewHire">
							<option value="0"SELECTED>NO</option>
							<option value="1">YES</option>
						</select></td></tr>
					<tr><td colspan="2" align="center"><input type="submit" name="CMD" value="Submit"></td></tr>
			</table>
			</form>
<%case else%>
    etc.....
 
Changed it to a Sub instead of a function and it will work: I have to run to a meeting so I can't explain why right now.
Code:
<script language="vbscript">
    [b][red]Sub[/red][/b] frm_onSubmit()
        Dim sMsg, nErr
        'MsgBox's are for testing
        If document.frmEmpInfo.txtFN.Value = "" Then
            sMsg = sMsg & "First name is required." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If document.frmEmpInfo.txtLN.Value = "" Then
            sMsg = sMsg & "Last name is required." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If document.frmEmpInfo.cboDept.Value = "" Then
            sMsg = sMsg & "Department is required." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If document.frmEmpInfo.txtTitle.Value = "" Then
            sMsg = sMsg & "Title is required." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If document.frmEmpInfo.txtExt.Value = "" Then
            sMsg = sMsg & "Extension is required." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If len(document.frmEmpInfo.txtExt.Value <> 4 Then
            sMsg = sMsg & "Extension must be 4 digits." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If Not isnumeric(document.frmEmpInfo.txtTitle.Value) Then
            sMsg = sMsg & "Extension must be a number." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If document.frmEmpInfo.cboCube.Value = "" Then
            sMsg = sMsg & "Cube is required." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        If len(document.frmEmpInfo.txtUserId.Value) > 8 Then
            sMsg = sMsg & "User ID's must be 8 or less characters." & VbCrLf
            MsgBox sMsg
            nErr = 1
        End If
        Select Case nErr 
            Case 1
                MsgBox sMsg,vbOKOnly,"Incorrect Information"
                window.event.returnValue = False
            Case Else
                MsgBox "No errors found"
                window.event.returnValue = True
        End Select
    [b][red]End Sub[/red][/b]</script>
 
PS. You don't need to qualify everything with "document." in a vbscript sub. FormName.Control.Value will usually do it.
 
Thanks for the tip about removing the document. that saves me some typing. I tried changing the function to a sub and that didn't work either, same results, no msgbox fires.

I went to another page that the script worked on and changed it from a Function to Sub and it still works great, this is the only page it's not firing on. Any other ideas.
 
The code looks OK to me. Try to change the button type and call the Function onClick event see if it will work:

<input type="submit" name="CMD" value="Submit">

to

<input type="button" name="CMD" value="Submit" onClick="frm_onSubmit()">

In the validation function, if there are no errors, submit the form like this:

document.frmEmpInfo.submit()
 
When using vbscript I usually add the call to the submit button <input type="Submit" Name="cmd" Value="submit" OnClick="frm_OnSubmit()"> to avoid typing the extra line frmEmpInfo.Submit() :)
On thing you might want to do after you do that is put an alert at the top of the sub do see if you're getting there e.g.
Code:
Sub frm_onSubmit()
      alert("I'm here!")
      window.event.returnvalue=false  
      Dim sMsg, nErr
      .........more code
End Sub
 
Hello cruford,

I would think you should take out the ";" on event handling.
[tt] onsubmit="frm_onSubmit()"[/tt]
rather than
[tt] onsubmit="frm_onSubmit()[red];[/red]" [green]_wrong?_[/green][/tt]

regards - tsuji
 
Veep, correct me if I'm wrong. If the button type is "submit", wouldn't the form be submited regardless the error?

 
Ok few things now I have tried removing the ";" and that doesn't change it. I also added a msgbox to the very first line of the sub and it does not fire, so the code isn't even processing. I tried adding this to the onClick of my submit button:

Code:
<input type="submit" name="CMD" value="Submit" onclick="frm_onSubmit();">

I have also tried:(notice the ; is gone)
Code:
<input type="submit" name="CMD" value="Submit" onclick="frm_onSubmit()">

I tried the same in my forms onSubmit event and still doesn't fire the sub.

I don't get why this works on every single page in my website except this one, would it help if i posted the entire page of code?
 
Kendel, if returning false, it will stop the form submission.

------------------------------------------------------------------------------------------------------------------------
If you don't have a sense of humor, you probably don't have any sense at all.
- Anonymous
 
Are you seeing an "error on page" message in the status bar when the pages loads? You may have other client-side script problems that are interfering with this sub. Go ahead and post the rest of it.
 
cruford, did you get any error? I think you might have some syntax error somewhere. Turn on the script debugging see if there is any.
 
cruford,

Take out ";" is definitely necessary (for calling vbscript function). If it still does not process, then use viewsource on the client and it will show what other goes wrong on the form-element construction.

- tsuji
 
Ok guys I appreciate the replies it was a syntax error in my script I was missing ")" from my LEN statement. I am going to give each of you a star because I got some good tips too. Thanks a ton.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top