2013-07-11 34 views
1

在验证I trim arguments.Value之前。如果验证失败,我想将修剪后的值放回输入。更改arguments.Value不起作用。 SenderClientValidationFunction中的第一个参数)是验证控制消息布局,而非原始输入。CustomValidator的ClientValidationFunction:有没有办法将修剪后的值设置回输入?

我看到唯一的方法:手动搜索输入元素的ID,或名称或类,但它使我的ClientValidationFunction知道某些输入,我必须设置ClientID或独特的类到我的输入。

,其为客户机验证功能的 val参数传递

回答

0

验证对象具有controltovalidate字段保持该服务器控制(在CustomValidatorControlToValidate属性指定)的唯一ID。所以,你可以轻松地设置值回你的控制如下:

function trimAndSetBack(val, args) { 
    // val.controltovalidate will always hold unique id 
    // of the server control so document.getElementById will work always 
    ValidatorSetValue(val.controltovalidate, ValidatorTrim(eventArgs.Value)); 
} 

function ValidatorSetValue(id, value) { 
    var control; 
    control = document.getElementById(id); 

    if (typeof (control.value) == "string" && (control.type != "radio" || control.checked == true)) { 
     control.value = value; 
     return true; 
    } 

    var i; 
    for (i = 0; i < control.childNodes.length; i++) { 
     if (ValidatorSetValue(control.childNodes[i], value)) { 
      return true; 
     } 
    } 

    return false; 
} 

ValidatorSetValue是标准ValidatorGetValueRecursive功能有点修改后的版本。如果您处理输入控件嵌套在其中的用户控件,则需要递归。

相关问题