validation doesn't fire

J

Jon Paal

validation doesn't fire

what's missing ?????


/////// ---- code -----///////////////////////////
Sub btnSubmit_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) 'Handles btnSubmit.Click
If page.isValid then
.....
end if


//////// --form --- ///////////////////////
<asp:TextBox id="x_Card_Num" runat="server" Width="250px"></asp:TextBox>
<asp:RequiredFieldValidator ID="x_Card_NumRequired" runat="server"
ControlToValidate="x_Card_Num"
ErrorMessage=""

ValidationGroup="orderform">*</asp:RequiredFieldValidator>

.....

<asp:validationSummary id="summary" runat="server"
ValidationGroup="orderform"
ShowmessageBox="False"
ShowSummary="True"
DisplayMode ="BulletList" />

<asp:ImageButton ID="btnSubmit" runat="server"
BorderWidth="0"
onclick="btnSubmit_Click"
ImageUrl="~/images/continue.gif" />
 
S

Scott M.

2 Things I see...

1. Your "Handles" clause on the btnSubmit event handler is commented so
there is a chance that this procedure isn't even being executed.
2. You don't need to write any code at all to get the
RequiredFieldValidator to work and usually don't need to check for
Page.IsValid because the client and server side validations would prevent
the page from processing.
 
J

Jon Paal

1. Your "Handles" clause on the btnSubmit event handler is commented so there is a chance that this procedure isn't even being
executed.

fortunately, other items in the procedure are working so I know the procedure is being processed....

2. You don't need to write any code at all to get the RequiredFieldValidator to work and usually don't need to check for
Page.IsValid because the client and server side validations would prevent the page from processing.

I guess that's where I'm stuck.
They're not firing ...The page goes through and ignores the required fields
 
T

Teemu Keiski

If the validator is in "orderform" validation group, the Button should be in
the same group also? E.g it should also have
ValidationGroup="orderform"?
 
S

Scott M.

The default action is to do server-side validation as well, for just this
reason. There is no need to check for Page.IsValid on standard validation
controls.
 
T

Teemu Keiski

Yes, it does validate but you do need to check Page.IsValid (or specific
validator's IsValid) to see if it passed or not (no one does that for you)
if you want to be 100% sure nothing gets pass the validation. That is, you
cannot rely solely on client-side validation.

Consider this example (TextBox, a couple of validators and a Button plus
Label):

<asp:TextBox ID="txtExpectingIntegers" runat="server" />
<asp:RequiredFieldValidator ID="reqINteger" runat="server"
ControlToValidate="txtExpectingIntegers" ErrorMessage="Insert something!" />
<asp:CompareValidator ID="cmpValInt" runat="server"
ControlToValidate="txtExpectingIntegers" Operator="DataTypeCheck"
Type="Integer" ErrorMessage="Please enter an integer!" />
<asp:Button ID="btnSend" runat="server" Text="Try it out"
OnClick="btnSend_Click" />

<asp:Label ID="lblMessage" runat="server" />

// Code

protected void btnSend_Click(object sender, EventArgs e)
{
//Testing for Page.IsValid here helps...
// if(!Page.IsValid)return;

try
{
int val = Int32.Parse(txtExpectingIntegers.Text);
lblMessage.Text ="We got an integer " + val.ToString();
}
catch(Exception ex)
{
lblMessage.Text = "Exception while parsing the integer:" +
ex.ToString();
}
}


If you now open it in in any normal browser with js enabled, of course with
standard scripts enabled it's catched & validated. But go now and disable
javascripts in your browser. Then type something non-number to the TextBox
and you'll see it blow, as Button's click is handled normally as if there
would be no validation. E.g nothing prevents the Button's click event from
running unless *you* check for the outcome of the validation (validation
message are displayed but page executes as-is). Think if it would be a
database operation dependant on the input user enters...

So to summarize: you do want to check IsValid at the server. Just running
validation isn't enough, you do need to know did it pass or not to decide
what's next (to let ValidationSummary display a nice message)

In case you don't still believe me, see this one:
http://msdn2.microsoft.com/en-us/library/7kh55542.aspx

There's this sentence

"ASP.NET performs validation on the server even if the validation controls
have already performed it on the client, so that you can test for validity
within your server-based event handlers. In addition, re-testing on the
server helps prevent users from being able to bypass validation by disabling
or changing the client script check."

Of course in normal intranet scenario these probably aren't issues, but I'd
think that being sure isn't too bad choice.

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top