Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple Form Validation

r0adhog
Guest
 
Posts: n/a
#1: Jul 19 '05
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



r0adhog
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Simple Form Validation


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


Jeff Cochran
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Simple Form Validation


On Tue, 18 May 2004 10:00:44 -0400, "r0adhog"
<roadhog@nospam.phreaker.net> wrote:
[color=blue]
>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?[/color]

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
r0adhog
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Simple Form Validation


Thanks for the input!

rh
"Jeff Cochran" <jcochran.nospam@naplesgov.com> wrote in message
news:40ac290d.577947124@msnews.microsoft.com...[color=blue]
> On Tue, 18 May 2004 10:00:44 -0400, "r0adhog"
> <roadhog@nospam.phreaker.net> wrote:
>[color=green]
> >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.[/color][/color]
I[color=blue][color=green]
> >am getting
> >a script error. Any help or any other suggestions on how to do what I am
> >attempting?[/color]
>
> 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[/color]


William Morris
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Simple Form Validation


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.

"r0adhog" <roadhog@nospam.phreaker.net> wrote in message
news:OBEzewOPEHA.3884@TK2MSFTNGP12.phx.gbl...[color=blue]
> Thanks for the input!
>
> rh
> "Jeff Cochran" <jcochran.nospam@naplesgov.com> wrote in message
> news:40ac290d.577947124@msnews.microsoft.com...[color=green]
> > On Tue, 18 May 2004 10:00:44 -0400, "r0adhog"
> > <roadhog@nospam.phreaker.net> wrote:
> >[color=darkred]
> > >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.[/color][/color]
> I[color=green][color=darkred]
> > >am getting
> > >a script error. Any help or any other suggestions on how to do what I[/color][/color][/color]
am[color=blue][color=green][color=darkred]
> > >attempting?[/color]
> >
> > 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[/color]
>
>[/color]


r0adhog
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Simple Form Validation


You are correct sir, I changed as you suggested and works great!

Thanks again!

rh

"William Morris" <news.remove.this.and.the.dots@seamlyne.com> wrote in
message news:2gurjsF73knmU1@uni-berlin.de...[color=blue]
> Something else to keep in mind: calling a function from the onclick event[/color]
of[color=blue]
> 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.
>
> "r0adhog" <roadhog@nospam.phreaker.net> wrote in message
> news:OBEzewOPEHA.3884@TK2MSFTNGP12.phx.gbl...[color=green]
> > Thanks for the input!
> >
> > rh
> > "Jeff Cochran" <jcochran.nospam@naplesgov.com> wrote in message
> > news:40ac290d.577947124@msnews.microsoft.com...[color=darkred]
> > > On Tue, 18 May 2004 10:00:44 -0400, "r0adhog"
> > > <roadhog@nospam.phreaker.net> wrote:
> > >
> > > >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"[/color][/color][/color]
onclick="ValForm()">[color=blue][color=green][color=darkred]
> > > ></form>
> > > ></body>
> > > ></html>
> > > >
> > > >As you can see, I am attempting to do some very simple form[/color][/color][/color]
validation.[color=blue][color=green]
> > I[color=darkred]
> > > >am getting
> > > >a script error. Any help or any other suggestions on how to do what[/color][/color][/color]
I[color=blue]
> am[color=green][color=darkred]
> > > >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[/color]
> >
> >[/color]
>
>
>[/color]


r0adhog
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Simple Form Validation


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


TomB
Guest
 
Posts: n/a
#8: Jul 19 '05

re: Simple Form Validation


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.



"r0adhog" <roadhog@nospam.phreaker.net> wrote in message
news:uPkAICOPEHA.1612@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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
>
>[/color]


Closed Thread


Similar ASP / Active Server Pages bytes