Solved

CustomValidator is not fired!

Posted on 2006-11-06
10
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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