开发者

Custom validator error text through javascript?

开发者 https://www.devze.com 2023-01-22 07:30 出处:网络
I want to set ASP.net custom validator error parameter text through client side javascript. How can access it via sender, arg开发者_开发知识库s parameters in my function?All you need to do is define t

I want to set ASP.net custom validator error parameter text through client side javascript. How can access it via sender, arg开发者_开发知识库s parameters in my function?


All you need to do is define the callback method in the ClientValidationFunction property of the CustomValidator definition:

<asp:CustomValidator id="CustomValidator1" 
   ...
   ClientValidationFunction="ClientValidationFunction" />

You can then define a client side validation script:

<script language="javascript">
function ClientValidationFunction(sender, args){

    var valid = false;
    // Validation logic..

    sender.errormessage = "Validation failed";

    args.IsValid = valid;
    return;        
}
</script>

Update: The sender variable holds a reference to the custom validator control - because JavaScript is dynamically typed, we can just update its errormessage property directly:

    sender.errormessage = "This is a new validation message";


This worked for me:

var clientValidationFunction = function(sender, args) {
    sender.textContent = sender.innerText = sender.innerHTML = "My new error text";
    // etc...
};

I just looked at the sender object and replaced all occurrences of the current error string, with the new error string.

0

精彩评论

暂无评论...
验证码 换一张
取 消