Simple Form Validation

R

r0adhog

I have a very simple form:

<html>
<head>
</head>
<body>
<%
function ValForm()
if len(document.form.newapp.all("AccessCode").Value) = 4 then
document.form.newapp.submit()
else
msgbox "Access Code in Improper Format"
end if
end function
%>
<form action="process.asp" method="Post">
<form name="newapp">
<input type="password" autocomplete="off" name="AccessCode"></td>
<input type="button" value="Submit" Name="Submit" onclick="ValForm()">
</form>
</body>
</html>

As you can see, I am attempting to do some very simple form validation. I
am getting
a script error. Any help or any other suggestions on how to do what I am
attempting?

Thanks,

rh
 
R

r0adhog

After an exhaustive research, here is what I came up with:

<html>
<head>
<script language="vbscript">
Sub newapp_onsubmit()
'msgbox len(document.newapp.all("AccessCode").Value)
' window.event.returnValue = false
if len(document.newapp.all("AccessCode").Value) < 4 then
msgbox "Improper Format"
window.event.returnValue = false
elseif msgbox("Cancel submit",vbYesNo) = vbYes then
window.event.returnValue = false
else
window.event.returnValue = true
end if
end Sub
</script>
</head>
<body>
<form name="newapp" action="process.asp" method="Post">
<input type="password" autocomplete="off" name="AccessCode"></td>
<input type="submit" value="Submit" Name="Submit">
</form>
</body>
</html>

Works! Looking for other suggestions as well.

Thanks,

rh
 
J

Jeff Cochran

I have a very simple form:

<html>
<head>
</head>
<body>
<%
function ValForm()
if len(document.form.newapp.all("AccessCode").Value) = 4 then
document.form.newapp.submit()
else
msgbox "Access Code in Improper Format"
end if
end function
%>
<form action="process.asp" method="Post">
<form name="newapp">
<input type="password" autocomplete="off" name="AccessCode"></td>
<input type="button" value="Submit" Name="Submit" onclick="ValForm()">
</form>
</body>
</html>

As you can see, I am attempting to do some very simple form validation. I
am getting
a script error. Any help or any other suggestions on how to do what I am
attempting?

Obligatory question: *What* script error?

Beyond that, the msgbox wouldn't pop up on the client side even if it
worked as you intended.

See:

http://www.aspfaq.com/show.asp?id=2198

(Which is likely the error you neglected to post...)

Jeff
 
W

William Morris

Something else to keep in mind: calling a function from the onclick event of
a submit element can cause unpredictable results - maybe the function gets
called, maybe it doesn't. I've had IE ignore function calls. Better to
change the SUBMIT to a BUTTON, and submit the form at the end of your
function, like you're doing.
 
R

r0adhog

Second Solution

<html>
<head>
<script language="vbscript">
Sub validform()
if len(document.newapp.all("AccessCode").Value) < 4 then
msgbox "Improper Format"
window.event.returnValue = false
elseif msgbox("Cancel submit",vbYesNo) = vbYes then
document.newapp.submit
else
window.event.returnValue = false
end if
end Sub
</script>
</head>
<body>
<form name="newapp" action="process.asp" method="Post">
<input type="password" autocomplete="off" name="AccessCode"></td>
<input type="button" value="Submit" Name="Submit" onclick="ValidForm()">
</form>
</body>
</html>

Thanks for the input folks!

rh
 
T

TomB

Just wanted to post the obvious, since no one mentioned it.

The code between <% and %> runs on the server. Your onClick events run on
the client (usually a browser)

So if you were to view the source of your code in the browser, you'd see no
"function ValForm()" hence the error on the browser.

Your solution uses VBScript on the CLIENT, so bear in mind that it probably
won't work in browsers other than IE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top