473,385 Members | 1,876 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,385 software developers and data experts.

CustomValidator problems

Hello,

I have an ASP.NET page which consists of a button (cmdSave), a label
(lblMessage), a text field (txtName) and a custom validator (vldName).

vldName is supposed to do both client *and* server validation of txtName;
the custom validation function is ValidateName, and the server validation
function is ValidateNameOnServer.

This validation should occur when cmdSave is clicked. If no text has been
entered, the validation should fail and an error message should be printed
on lblMessage; else the validation should succeed, and lblMessage should be
blank.

Unfortunately, I am having problems triggering both ValidateName or
ValidateNameOnServer. The alert won't go off in ValidateName, and when I put
a breakpoint in ValidateNameOnServer, it isn't hit.

Heres the code for the page:

<HTML>
<HEAD>

...

<script type="text/javascript">
function ValidateName(sender, args)
{
alert("In ValidateName.");
//var elem = getElementById("<% =lblMessage.ClientID %>");
if (args.Value.length != 0)
{
//elem.Value = "";
args.IsValid = true;
return true;
}
else
{
//elem.Value = "Name is required.";
args.IsValid = false;
return false;
}
}
</script>
</HEAD>

<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtName" style="Z-INDEX: 105; LEFT: 107px; POSITION:
absolute; TOP: 38px" runat="server"></asp:TextBox>
<asp:Button id="cmdSave" style="Z-INDEX: 113; LEFT: 218px; POSITION:
absolute; TOP: 74px" runat="server" Text="Save"
OnClick="cmdSave_Click"></asp:Button>
<asp:CustomValidator ID="vldName" runat="server"
style="position:absolute; left: 268px; top: 39px;"
ErrorMessage="Name cannot be blank;"
ClientValidationFunction="ValidateName"
OnServerValidate="ValidateNameOnServer"
ControlToValidate="txtName">*</asp:CustomValidator>
<asp:Label ID="lblMessage" runat="server" style="position:absolute;
left: 104px; top: 122px;" Width="253px"></asp:Label>
</form>
</body>
</HTML>

Here's the code for ValidateNameOnServer in the codebehind:

protected void ValidateNameOnServer(object source,
ServerValidateEventArgs args)
{
if (args.Value.Length == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
I'd appreciate it if anyone could shed any light on what I am doing wrong,
or what I am missing out.

Epetruk
Sep 14 '08 #1
2 1599
Any one have any ideas about this issue?

Epetruk

"Epetruk" <no****@blackhole.comwrote in message
news:O8*******************@newsfe16.ams2...
Hello,

I have an ASP.NET page which consists of a button (cmdSave), a label
(lblMessage), a text field (txtName) and a custom validator (vldName).

vldName is supposed to do both client *and* server validation of txtName;
the custom validation function is ValidateName, and the server validation
function is ValidateNameOnServer.

This validation should occur when cmdSave is clicked. If no text has been
entered, the validation should fail and an error message should be printed
on lblMessage; else the validation should succeed, and lblMessage should
be blank.

Unfortunately, I am having problems triggering both ValidateName or
ValidateNameOnServer. The alert won't go off in ValidateName, and when I
put a breakpoint in ValidateNameOnServer, it isn't hit.

Heres the code for the page:

<HTML>
<HEAD>

...

<script type="text/javascript">
function ValidateName(sender, args)
{
alert("In ValidateName.");
//var elem = getElementById("<% =lblMessage.ClientID %>");
if (args.Value.length != 0)
{
//elem.Value = "";
args.IsValid = true;
return true;
}
else
{
//elem.Value = "Name is required.";
args.IsValid = false;
return false;
}
}
</script>
</HEAD>

<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtName" style="Z-INDEX: 105; LEFT: 107px; POSITION:
absolute; TOP: 38px" runat="server"></asp:TextBox>
<asp:Button id="cmdSave" style="Z-INDEX: 113; LEFT: 218px; POSITION:
absolute; TOP: 74px" runat="server" Text="Save"
OnClick="cmdSave_Click"></asp:Button>
<asp:CustomValidator ID="vldName" runat="server"
style="position:absolute; left: 268px; top: 39px;"
ErrorMessage="Name cannot be blank;"
ClientValidationFunction="ValidateName"
OnServerValidate="ValidateNameOnServer"
ControlToValidate="txtName">*</asp:CustomValidator>
<asp:Label ID="lblMessage" runat="server" style="position:absolute;
left: 104px; top: 122px;" Width="253px"></asp:Label>
</form>
</body>
</HTML>

Here's the code for ValidateNameOnServer in the codebehind:

protected void ValidateNameOnServer(object source,
ServerValidateEventArgs args)
{
if (args.Value.Length == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
I'd appreciate it if anyone could shed any light on what I am doing wrong,
or what I am missing out.

Epetruk

Sep 15 '08 #2

"Epetruk" <no****@blackhole.comwrote in message
news:KH*******************@newsfe25.ams2...
Any one have any ideas about this issue?

Epetruk

"Epetruk" <no****@blackhole.comwrote in message
news:O8*******************@newsfe16.ams2...
>Hello,

I have an ASP.NET page which consists of a button (cmdSave), a label
(lblMessage), a text field (txtName) and a custom validator (vldName).

vldName is supposed to do both client *and* server validation of txtName;
the custom validation function is ValidateName, and the server validation
function is ValidateNameOnServer.

This validation should occur when cmdSave is clicked. If no text has been
entered, the validation should fail and an error message should be
printed on lblMessage; else the validation should succeed, and lblMessage
should be blank.

Unfortunately, I am having problems triggering both ValidateName or
ValidateNameOnServer. The alert won't go off in ValidateName, and when I
put a breakpoint in ValidateNameOnServer, it isn't hit.

Heres the code for the page:

<HTML>
<HEAD>

...

<script type="text/javascript">
function ValidateName(sender, args)
{
alert("In ValidateName.");
//var elem = getElementById("<% =lblMessage.ClientID %>");
if (args.Value.length != 0)
{
//elem.Value = "";
args.IsValid = true;
return true;
}
else
{
//elem.Value = "Name is required.";
args.IsValid = false;
return false;
}
}
</script>
</HEAD>

<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtName" style="Z-INDEX: 105; LEFT: 107px; POSITION:
absolute; TOP: 38px" runat="server"></asp:TextBox>
<asp:Button id="cmdSave" style="Z-INDEX: 113; LEFT: 218px; POSITION:
absolute; TOP: 74px" runat="server" Text="Save"
OnClick="cmdSave_Click"></asp:Button>
<asp:CustomValidator ID="vldName" runat="server"
style="position:absolute; left: 268px; top: 39px;"
ErrorMessage="Name cannot be blank;"
ClientValidationFunction="ValidateName"
OnServerValidate="ValidateNameOnServer"
ControlToValidate="txtName">*</asp:CustomValidator>
<asp:Label ID="lblMessage" runat="server" style="position:absolute;
left: 104px; top: 122px;" Width="253px"></asp:Label>
</form>
</body>
</HTML>

Here's the code for ValidateNameOnServer in the codebehind:

protected void ValidateNameOnServer(object source,
ServerValidateEventArgs args)
{
if (args.Value.Length == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
I'd appreciate it if anyone could shed any light on what I am doing
wrong, or what I am missing out.

Epetruk

Still no answer...

Anyway, I found out some more - I notice that the client validation function
(ValidateName) *is* triggered when I enter a value in the text box (that is
supposed to be validated), but it is *not* triggered when the text box is
empty. (I found out by putting alerts.)

I also found that no matter what I do, the server validation function
(ValidateNameOnServer) is not being fired. I removed all the client
validation, but it made no difference - it still didnt fire.

I'd really, really appreciate it if someone could help. I'm sure it's
something fairly straightforward.

E.
Sep 22 '08 #3

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...
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: SMG | last post by:
Hi All. My forms has two textboxes, 1 username, 2 password. Both has requiredfield validator it works fine when there is no input in these textboxes. And the errorMsg is shown in...
0
by: John Bonds | last post by:
I'm having some problems with implementing client-side validation in a user control. Here' my code for the usercontrol: <%@ Control Language="vb" AutoEventWireup="false"...
0
by: ghafranabbas | last post by:
This is how you use the customvalidator control in any INamingContainer interface control (Datagrid, DataList, DataRepeater, etc). 1. In the ItemTemplate, place your customvalidator 2. Set the...
1
by: Beffmans | last post by:
Hi I have defined an customvalidator on my TextBox: function clientvalidate(source, arguments){ { // even number? if (arguments.Value%2 == 0) arguments.IsValid = true; else
1
by: David | last post by:
I need help with CustomValidator in 1.1. I added the CustomValidator control and code as per the doc., (See below) However, the code is never executed. Is there an extra switch or setting to...
1
by: theresa | last post by:
I'm about at my wits end trying to figure out why this isn't working, so I'm hoping one of you can help! I'm sure there must be something simple I'm missing. I have a checkbox on an Asp.NET 2.0...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?

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.