2016-03-17 53 views
-1

我有一个webForms网站,它使用asp CustomFieldValidator来检查文本在文本框中是否发生了变化。当它失败时,我希望文本框返回到其默认文本。有没有办法做到这一点?如何验证失败时将aspTextBox恢复为默认值

<asp:CustomValidator ID="PasswordChangedValidator" runat="server" 
     ErrorMessage="message" ControlToValidate="PASSWORDTb" 
     ClientValidationFunction="passwordValidate()" Style="color: red;" 
     OnServerValidate="PasswordChangedValidator_ServerValidate" 
     SetFocusOnError="true"></asp:CustomValidator> 

function passwordValidate() { 
    var pswdText = doc.getElementById('<%=PASSWORDTb.ClientID %>').value; 
    if (pswdText.indexOf('●') >= 0) { 
     if (pswdText == "●●●●●●●●") { 
      return true; 
     } 
     doc.getElementById('<%=PASSWORDTb.ClientID %>').value = '●●●●●●●●'; 
     return false; 
    } 
} 

有没有办法,我失去了一些东西很明显: 我在服务器验证码

protected void PasswordChangedValidator_ServerValidate(object source, ServerValidateEventArgs args) 
{ 
    if (PASSWORDTb.Text.Contains("●") && PASSWORDTb.Text != "●●●●●●●●") 
    { 
     args.IsValid = false; 
     PASSWORDTb.Text = "●●●●●●●●"; 
    } 
    else { args.IsValid = true; } 
} 

和客户方的代码试过吗?

+0

你必须设置它注释掉的代码://PASSWORDTb.Text = “●●●●●●●●”; –

+1

不是100%确定你想要在这里实现的目标,但是我可以在你的文本框中使用'placeholder'作为替代选择。 IE浏览器。 'MyTextBox.Attributes.Add(“占位符”,“输入密码”);'那么你不需要担心验证该值为“输入密码”将出现在文本框中,而它是空白的。 –

+0

@RudyTheHunter对不起,忘记取消注释本文的目的。我编辑过,以反映我实际尝试的内容。 –

回答

0

感谢@Hugo Yates指引我在属性正确的方向。 我的解决方案是将passwordChanged()添加到我的TextBox的onblur属性中。

<asp:TextBox ID="PASSWORDTb" OnBlur="javascript:passwordChanged()"...></asp:TextBox> 

<script type="text/javascript"> 
    function passwordChanged(){ 
     if (pswdText.indexOf('●') >= 0) { 
      if (pswdText != "●●●●●●●●") { 
      doc.getElementById('<%=PASSWORDTb.ClientID %>').value = '●●●●●●●●'; 
      } 
     } 
</script>