最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check result of ASP.Net validator clientside - Stack Overflow

programmeradmin1浏览0评论

I know the built-in ASP.Net validators e with a client-side framework, however I've been unable to find anything that lets me check a single validator for it's Valid state.

I expect it to be possible though, so I hope someone in here knows how to do it :-)

The validator in question is a RegularExpressionValidator, which I use to determine whether an e-mail address is valid or not.

Here's some brief code:

<script>
function CheckForExistingEmail()
{
  Page_ClientValidate(); // Ensure client validation
  if (revEmail.IsValid) // pseudo code!
  {
    // Perform server side lookup in DB for whether the e-mail exists.
  }
}
</script>

<asp:TextBox runat="server" id="tbEmail" onblur="CheckForExistingEmail();" />
<asp:RegularExpressionValidator id="revEmail" runat="server" ControlToValidate="tbEmail" ErrorMessage="Not a valid e-mail address" ValidationExpression="([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})" />

I know the built-in ASP.Net validators e with a client-side framework, however I've been unable to find anything that lets me check a single validator for it's Valid state.

I expect it to be possible though, so I hope someone in here knows how to do it :-)

The validator in question is a RegularExpressionValidator, which I use to determine whether an e-mail address is valid or not.

Here's some brief code:

<script>
function CheckForExistingEmail()
{
  Page_ClientValidate(); // Ensure client validation
  if (revEmail.IsValid) // pseudo code!
  {
    // Perform server side lookup in DB for whether the e-mail exists.
  }
}
</script>

<asp:TextBox runat="server" id="tbEmail" onblur="CheckForExistingEmail();" />
<asp:RegularExpressionValidator id="revEmail" runat="server" ControlToValidate="tbEmail" ErrorMessage="Not a valid e-mail address" ValidationExpression="([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})" />
Share Improve this question asked Dec 28, 2010 at 14:26 SteffenSteffen 14k7 gold badges59 silver badges70 bronze badges 2
  • Your code should work fine if you take the clientId of the regularexpressionvalidator and check IsValid like you do. – Pabuc Commented Dec 28, 2010 at 14:29
  • I'm not sure I follow you, I've tried this: <%= revEmail.ClientID %>.IsValid and document.getElementById('<%= revEmail.ClientID %>').IsValid. Both are undefined :-( – Steffen Commented Dec 28, 2010 at 14:46
Add a ment  | 

2 Answers 2

Reset to default 6

I found a way around it myself now:

By adding a ValidationGroup to the validator, I can use Page_ClientValidate(validationgroup) - which returns a bool value.

I'm not sure if it was the same thing you meant Pabuc, if it was please drop an answer and I'll obviously select that as the correct one :-)

Here's the code which works:

<script>
function CheckForExistingEmail()
{
  if(Page_ClientValidate("email"))
  {
    // Perform server side lookup in DB for whether the e-mail exists.
  }
}
</script>

<asp:TextBox runat="server" id="tbEmail" onblur="CheckForExistingEmail();" />
<asp:RegularExpressionValidator id="revEmail" runat="server" ValidationGroup="email" ControlToValidate="tbEmail" ErrorMessage="Not a valid e-mail address" ValidationExpression="([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})" />

You can look at the visibility of the Validator message (we usually have a red Asterisk *)

if (document.getElementById("ctl00_ContentPlaceHolder1_revClientSite").style.visibility == "hidden") {
  // validator says go ahead
} else {
  alert("please fix the indicated field - it is not valid");
}

.style.visibility will be "hidden" or "visible"

发布评论

评论列表(0)

  1. 暂无评论