CustomValidator does not work

I have a CustomValidator that checks if the text entered in text fields matches the specific fields in the database. All this worked fine before, but since then I changed my page a bit and it doesn’t work anymore. I did not think that I changed everything that would affect this, but apparently I did. All my other validators (required validator fields) work correctly, but my CustomValidator is not responding.

Anyway, here is my code:

CustomValidator:

<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken."></asp:CustomValidator> 

VB codebehind:

 Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate 'Checking for duplicate course numbers 'get values Dim checkPrefix = txtCoursePrefix.Text Dim checkNum = txtCourseNum.Text 'db connectivity Dim myConn As New OleDbConnection myConn.ConnectionString = AccessDataSource2.ConnectionString myConn.Open() 'select records Dim mySelect As New OleDbCommand("SELECT 1 FROM tableCourse WHERE prefix=? AND course_number=?", myConn) mySelect.Parameters.AddWithValue("@checkPrefix", checkPrefix) mySelect.Parameters.AddWithValue("@checkNum", checkNum) 'execute(Command) Dim myValue = mySelect.ExecuteScalar() 'check if record exists If myValue IsNot Nothing Then CustomValidator1.SetFocusOnError = True args.IsValid = False End If End Sub 

Everything works up to CustomValidator1.SetFocusOnError = True and args.IsValid = False. I tested the If statement, and it works correctly, it returns true, and everything I do inside it is executed.

+4
source share
5 answers

Things you should know when using customvalidators:

If you are testing using a ValidationGroup , be sure to add it to your CustomValidator .

Set the ControlToValidate property.

A CustomValidator control never fires when the ControlToValidate control is empty unless you set ValidateEmptyText=true .

When using ClientValidationFunction="customClientValidationFunction" use the following signature:

 function customClientValidationFunction(sender, arguments) { arguments.IsValid = true; //validation goes here } 
+7
source

You must set the ValidateEmptyText="true" property to CustomValidator . Client and server functions will always be called in this case.

He solved the problem for me.

+3
source

If the handler receives the call, and you successfully set the args.IsValid argument to false, then what it does is set the Page.IsValid parameter to false. But, unfortunately, this does not prevent the form from being clogged. What you need to do is check that the Page.IsValid property is in your code that processes the submit form, for example, in the submit button handler.

So, in addition to the code you posted that sounds like it is working correctly, make sure you have something similar for your submit handler (C # example):

 protected void btnSubmit_Click(object sender, EventArgs e) { if (!Page.IsValid) { // by simply returning, the error message for the CustomValidator will be displayed return; } // do processing for valid form here } 
+1
source

Use this

OnServerValidate = "CustomValidator1_ServerValidate"

as an example, and it will work.

 <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken." OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator> 

Gaurav Agrawal

0
source

First of all, put the validation group on the validators and the button. If this also does not work, set OnClientClick = 'CheckValidate ();' and declare a function that will call the page_clientvalidate method along with the parameter. verification team. That will surely work. If this does not work, put the debugger in the javascript method and debug the same ones

0
source

All Articles