Validation Controls not stopping Form Post

J

John Kotuby

Hi all,

I was under the impression that the Validation Server Controls actually
perform validation on the Client and don't allow a form to Post if any of
the validations fail. Please correct me if I am wrong about that. If that is
the case then I will probably have to write my own validators in JavaScript
(which I thought that ASP.NET 2.0 would help me to avoid).

I have a form for filling out Email information which contains validation
controls. In fact I have 2 forms like that with the same validator controls
pointing to the same input fields. They just operate slightly differently
upon post.

One of them seems to work properly and the other one doesn't and I cant seem
to figure out why.

The Form1 that works (validates) properly is posted by the button as below:

<asp:Button runat="server" ID="btnSubmit" Text="Preview"
PostBackUrl="~/Common/EmailList.aspx" ToolTip="Preview Email" />


This Form1 cross posts to another page which shows a preview of the HTML to
be sent in the email and then sends it when the user clicks a "Send" button.


The Form2 that doesn't work (such that it allows the email to be sent
without a subject or email addresses) is posted by the button control below:

<asp:Button runat="server" ID="btnSend" Text="Send" OnClientClick="return
SendMail();" OnClick="SendIt" CausesValidation="true" ></asp:Button>

OnClientClick calls a Javascript function that assembles the Body content
(actually HTML) and places it a hidden field before the form is posted. That
part works fine and the HTML email sends OK. But, the Validation controls
don't seem to work.

Form2 posts back to itself, where it then runs the "SendIt" method that
actually sends the email.
The other difference is the OnClientClick javascript that is called. This is
the form where validation does not operate properly, meaning the
OnClick="SendIt" method is fired even if the form fields do not pass the
validation tests.

Can anyone tell me why the Validation appears to work in one case and not
the other? I have triple checked that all validators are associated with the
correct controls. In fact Form2 was built by copying much of the code from
Form1.

Thanks for any responses.
 
G

George Ter-Saakov

It's responsobillity of the code to check if page is valid befor doing any
action....

So your Button_OnClick code should have folowing code as first lines....

if( !this.IsValid)
return;
.........

George
 
M

Mark Rae [MVP]

I was under the impression that the Validation Server Controls actually
perform validation on the Client and don't allow a form to Post if any of
the validations fail. Please correct me if I am wrong about that.
http://msdn2.microsoft.com/en-us/library/yb52a4x0.aspx

I will probably have to write my own validators in JavaScript (which I
thought that ASP.NET 2.0 would help me to avoid).

FWIW, I never ever use the Validation controls...

I have a whole suite of client-side JavaScript validation routines which I
find give me much more control over the entire client-side validation
process...
 
S

Scott Roberts

John Kotuby said:
Hi all,

I was under the impression that the Validation Server Controls actually
perform validation on the Client and don't allow a form to Post if any of
the validations fail. Please correct me if I am wrong about that. If that
is the case then I will probably have to write my own validators in
JavaScript (which I thought that ASP.NET 2.0 would help me to avoid).

I have a form for filling out Email information which contains validation
controls. In fact I have 2 forms like that with the same validator
controls pointing to the same input fields. They just operate slightly
differently upon post.

One of them seems to work properly and the other one doesn't and I cant
seem to figure out why.

The Form1 that works (validates) properly is posted by the button as
below:

<asp:Button runat="server" ID="btnSubmit" Text="Preview"
PostBackUrl="~/Common/EmailList.aspx" ToolTip="Preview Email" />


This Form1 cross posts to another page which shows a preview of the HTML
to be sent in the email and then sends it when the user clicks a "Send"
button.


The Form2 that doesn't work (such that it allows the email to be sent
without a subject or email addresses) is posted by the button control
below:

<asp:Button runat="server" ID="btnSend" Text="Send" OnClientClick="return
SendMail();" OnClick="SendIt" CausesValidation="true" ></asp:Button>

OnClientClick calls a Javascript function that assembles the Body content
(actually HTML) and places it a hidden field before the form is posted.
That part works fine and the HTML email sends OK. But, the Validation
controls don't seem to work.

Form2 posts back to itself, where it then runs the "SendIt" method that
actually sends the email.
The other difference is the OnClientClick javascript that is called. This
is the form where validation does not operate properly, meaning the
OnClick="SendIt" method is fired even if the form fields do not pass the
validation tests.

Can anyone tell me why the Validation appears to work in one case and not
the other? I have triple checked that all validators are associated with
the correct controls. In fact Form2 was built by copying much of the code
from Form1.

Thanks for any responses.

It sounds like you only want "SendMail()" to run if the page is valid. Try
this link:

http://forums.asp.net/p/1011848/1354154.aspx
 
J

John Kotuby

George,

Thanks for reminding me of the importance of checking the validity of the
submitted form at the Server, should the user bypass any JavaScript or
otherwise client-side validation. I have implemented your suggestion.

It did solve the problem of the Email being sent with invalid information,
but it required a PostBack and a complete form life-cycle, including another
trip to the database to gather (again) necessary display elements.

See my response to Mark Rae for the final solution, which I hope will help
others who might run across a similar situation.

Thanks again... John
 
J

John Kotuby

Scott,

Thanks for your suggestion. I implemented it in a slightly different manner.
Instead of what I had origianlly done:

OnClientClick="return SendMail();"

I placed that call in the form onSubmit event instead, so that I could still
gather up the on-screen InnerHtml right before the post. See my response to
Mark Rae for more info on how I solved the problem.

Essentially, though, by making the call to my JavaScript function and
returning "true" I was client-hijacking the Microsoft generated
DoPostbackWithOptions call that followed my Javascript as the 2nd command in
the HTML onClick event...such that the form was submitted without
client-side Validation ever being run.
 
J

John Kotuby

Many thanks Mark,

The link to the explanation of the client-side workings of the Validation
Server controls finally led me to a solution.
I had placed a "filter" on the help searches in MSDN for VS2005 so it only
returned articles for "Web Development".
Apparently, whoever decides what the filters do made it difficult for me to
find the article you pointed me to. From now on I leave the Help
"unfiltered".

Anyway, it was my OnClientClick="return SendMail();" that turned out ot be
the culprit.
It was being called right before the Microsoft generated
DoPostBackWithOptions client-side call, which of course runs the client-side
validation routines. The form was being submitted without client-side
validation, thus underscoring the need for Server-side form validation at
all times.

I moved the call to my function "SendMail();" from the Button OnClientClick
event to the Form onSubmit event so that it gets called only if client-side
validation is successful.

FWIW -- I am now well on my way to completing my own JavaScript validation
library, thanks to a handy tool I downloaded -- RegexBuddy -- to help me
with Regular Expressions. I highly recommend the tool for only about $40.

Thanks again
 
M

Mark Rae [MVP]

FWIW -- I am now well on my way to completing my own JavaScript validation
library

Glad to hear it!

Once you have it the way you like it, you'll never use client-side
Validation Controls again...
 

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,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top