Solved

CustomValidator is not fired!

Posted on 2006-11-06
10
475 Views
Last Modified: 2007-12-19
Hi,  CustomValidator control is not fired when I submit the form, here is the code:


<asp:CustomValidator ControlToValidate="Departments" OnServerValidate="HasDepartment" Text="Employee should be assigned to a department" runat="server" ID="Departmentsvld" />


   Public Sub HasDepartment(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        Dim blnHasDepartment As Boolean
        blnHasDepartment = False
        Dim LiThisOne As ListItem
        For Each LiThisOne In Departments.Items
            If LiThisOne.Selected Then
                blnHasDepartment = True
            End If
        Next
        If blnHasDepartment = True Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If

    End Sub

Do someone know what I am doing wrong?
0
Comment
Question by:Abdu_Allah
  • 5
  • 3
  • 2
10 Comments
 
LVL 13

Assisted Solution

by:jeebukarthikeyan
jeebukarthikeyan earned 100 total points
ID: 17881214
hi,

i will site a sample example

<asp:CustomValidator runat="server" id="custPrimeCheck"
ControlToValidate="txtPrimeNumber"
OnServerValidate="PrimeNumberCheck"
ErrorMessage="Invalid Prime Number" />

<script language="JavaScript">
<!--
  function CheckPrime(sender, args)
  {
    var iPrime = parseInt(args.Value);
    var iSqrt = parseInt(Math.sqrt(iPrime));

    for (var iLoop=2; iLoop<=iSqrt; iLoop++)
      if (iPrime % iLoop == 0)
      {
         args.IsValid = false;
         return;
      }

    args.IsValid = true;
  }
// -->
</script>

may be ur problem lies in the script

b u d d h a


0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881425
I cannot see anything that may cause a problem!
0
 
LVL 13

Expert Comment

by:jeebukarthikeyan
ID: 17881520
hi,

can u just put ur javascript here

b u d d h a
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881570
What javascript?
0
 
LVL 13

Expert Comment

by:jeebukarthikeyan
ID: 17881664
hi,
the validation script

b u d d h a
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881695
It is in the question body.
Here it is again:

 Public Sub HasDepartment(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        Dim blnHasDepartment As Boolean
        blnHasDepartment = False
        Dim LiThisOne As ListItem
        For Each LiThisOne In Departments.Items
            If LiThisOne.Selected Then
                blnHasDepartment = True
            End If
        Next
        If blnHasDepartment = True Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If

    End Sub
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881702
I used server side validation script and not client-side.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17883070
If you are using IE to test the validation, a custom validator with no "OnClientValidate" it will not fire until a postback happens. If you want it to fire when the controltovalidate loses focus then you need to have the javascript function on the page that will do client side validation.
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17884716
So how can I check if the listbox has a selected item(s) or not using javascript?
0
 
LVL 28

Accepted Solution

by:
strickdd earned 150 total points
ID: 17888621
for(int i=0; i<document.GetElementById("ListBoxIDHere").length; i++)
{
     if(document.GetElementById("ListBoxIDHere")[i].selected)
     {
        //this is selected
     }
}


The syntax is close. I would worry most about my capitalization.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript error 1 53
ISS migration from IIS 6 to IIS 8.5 rendering problems 4 55
How to LOAD up a .NET Framework website with data? 2 42
Error in page 3 46
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now