0

I am trying to trigger asp.net custom validator with java script but it is not triggering. Other validators such as required field validators are getting triggered from the below-shown code. Can anyone tell me what is wrong in the below code or why it is not triggering

<script type="text/javascript">
    function CheckValidation(btn) {
        var valid = false;
        if (Page_ClientValidate('RtRegValGrp')) {
            alert('valid');
            __doPostBack(btn.name, "");
         } else {
             alert('not valid');
         }
     }
</script>

<asp:TextBox ID="txtCompanyName" runat="server" MaxLength="100"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ControlToValidate="txtCompanyName" 
    ValidationGroup="RtRegValGrp" 
    ErrorMessage="Company name is required !"
    ForeColor="red" Display="Dynamic" />
<asp:CustomValidator ID="txtCompanyNameValidator" runat="server"
    ControlToValidate="txtCompanyName"
    ValidationGroup="RtRegValGrp"
    OnServerValidate="BlackListValidator" 
    ForeColor="Red" Display="Dynamic">
</asp:CustomValidator>

<asp:Button ID="Button1" runat="server" CssClass="goldBtn" Text="SUBMIT" 
    OnClientClick="CheckValidation(this); return false;" 
    OnClick="Button1_Click" />

//server side code
protected void BlackListValidator(object source, ServerValidateEventArgs args)
{
    args.IsValid = false;
}

0

1 Answer 1

2

Your CustomValidator is currently set up with server-side validation code only... there is no way for the client-side ASP.Net code to be able to know the result of the validation without a post-back.

For it to be client-side, you need to replicate the validation code in javascript (you must leave the server-side code in place for security reasons) and then set the ClientValidationFunction property...

<asp:CustomValidator ValidationGroup="RtRegValGrp" ID="txtCompanyNameValidator"
  ForeColor="Red" OnServerValidate="BlackListValidator" Display="Dynamic"
  ControlToValidate="txtCompanyName" runat="server"
  ClientValidationFunction="BlackListValidatorFnc" />

<script>
  function BlackListValidatorFnc(src, args) {
    args.IsValid = false;
  }
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

But the validators such as requiredfieldvalidator is getting triggered from below code if (Page_ClientValidate('RtRegValGrp')) without postback.
Yes, because things like RequiredFieldValidator have client-side code built into ASP.Net. A custom validator is exactly that... it's custom. In the same way that you've had to write server-side code to do your specific checking, you have to replicate that to have client-side. Sorry, but I don't know how else to describe it to you
ok thanks for the info. Is there any other way to trigger server side validation with java script (with postback).
You could do something with AJAX, but unless it's something that can only happen on the server (e.g. DB related) then it would make more sense to replicate it in the javascript.
I have to ask why you're doing the javascript anyway? Your code seems unnecessarily complicated and flawed. I can see no reason why you can't add the ValidationGroup to the button, and then use an <asp:ValidationSummary> element to display messages. That would then do all the client-side validation as it should, and when client-side has passed, the post-back is fired and your server-side validation is done?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.