[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

CustomValidator is not fired!

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
Abdu_Allah
Asked:
Abdu_Allah
  • 5
  • 3
  • 2
2 Solutions
 
jeebukarthikeyanCommented:
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
 
Abdu_AllahAuthor Commented:
I cannot see anything that may cause a problem!
0
 
jeebukarthikeyanCommented:
hi,

can u just put ur javascript here

b u d d h a
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Abdu_AllahAuthor Commented:
What javascript?
0
 
jeebukarthikeyanCommented:
hi,
the validation script

b u d d h a
0
 
Abdu_AllahAuthor Commented:
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
 
Abdu_AllahAuthor Commented:
I used server side validation script and not client-side.
0
 
strickddCommented:
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
 
Abdu_AllahAuthor Commented:
So how can I check if the listbox has a selected item(s) or not using javascript?
0
 
strickddCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now