473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A CustomValidator Problem, I don't understand the logic

The full code is pasted below. It looks scarily long, but it's pretty
simple. It is adapted from a sample code at MSDN.

If you run it, and check it out from your browser, you'll see:

CustomValidator ServerValidate Example
Enter an even number:
[_________________]

Zip: [______________]

[Validate!]

I want to check if the number is valid only if the zip is valid.
That's why I have

If vldZip.IsValid Then

'Check if the number entered is even.

End If

But, the number is checked even if the zip is invalid. Try "asdf" for
the Zip and 13 for the even number, and you'll see what I meant.

I don't understand this, my logic in the code seems to be correct.

Please help. Thanks a lot!

********The fully workable code below************

<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script runat="server">
Sub ValidateBtn_OnClick(sender As Object, e As EventArgs)
If vldZip.IsValid Then
Message.Text = "Zip is valid."
Else
Message.Text = "Zip is not valid!"
End If

End Sub

Sub ServerValidation(source As Object, args As
ServerValidateEventArgs)
If vldZip.IsValid() Then
Try
' Test whether the value entered into the text box is even.
Dim num As Integer = Integer.Parse(args.Value)
args.IsValid = ((num mod 2) = 0)
Catch ex As Exception
args.IsValid = false
End Try
End If
End Sub
</script>

<title>Custom Validator Test</title></head>
<body>
<form runat="server">
<h3>CustomValidator ServerValidate Example</h3>
<asp:Label id="Message"
Text="Enter an even number:"
Font-Name="Verdana"
Font-Size="10pt"
runat="server" />

<p>
<asp:TextBox id="Text1"
runat="server" />

<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Name="verdana"
Font-Size="10pt"
OnServerValidate="ServerValidation"
runat="server"/>
<p>
Zip:
<asp:RegularExpressionValidator
ControlToValidate="txtZip"
Display="Dynamic"
EnableClientScript="false"
ErrorMessage="Invalid Zip! <br />"
ID="vldZip"
runat="server"
ValidationExpression="[0-9]{5}" />

<asp:TextBox ID="txtZip"
Columns="5"
MaxLength="5"
TextMode="SingleLine"
runat="server" />
<p>

<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>
</form>
</body>
</html>

Nov 19 '05 #1
0 1249

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jcpmeticulus | last post by:
Hi I've spent the last day or so debugging a problem with a CustomValidator and am now totally stumped! Basically I use a number of CustomValidator's on my page, but have cut this down now to...
10
by: Mr. x | last post by:
Hello, I have some code as follows. It seems that customValidator doesn't work - because i don't get any message. In the body : <asp:table> <asp:TableRow>
5
by: Gene McCrory | last post by:
Is it possible to have a CustomValidator against two controls in an EditItemTemplate on a DataGrid? Case: Have a business rule that states if ATextBox starts with "ACertainValue" then BTextBox...
2
by: Stephen Miller | last post by:
Can the CustomValidator be used to simply report unexpected errors, without requiring Client/Server validation? To explain, say you had a simple text box and button that did a Full-text Search of a...
3
by: Ronan Dodworth | last post by:
Hi there I'm having a little bit of a problem with my customvalidator control. The problem is the javascript runs fine on my local webserver IIS but not when I post it to the web hosting server....
1
by: Vi | last post by:
Hi, I have a TextBox, Button and CustomValidator web controls on an aspx page. When a user clicks the Button, I want to perform server side validation of the textbox with the method specified in...
11
by: ASP newbie | last post by:
I cannot run my asp.net application in w2k server. But the program works fine under w2k professional. Can anyone tell me is there any difference in the settings? Many thanks.
0
by: Don | last post by:
I have a page with a GridView that uses an ObjectDataSource. The user can change some parameters on the page and refresh the GridView with a button click. One of the parameters has a...
2
by: Jeff | last post by:
hey net 3.5 I have problem with a customvalidator. I enter values into the TextBox named "txt" and clicks on the save button (ibSave) then the TestValidate method get triggered. TestValidate...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.