4

I know the built-in ASP.Net validators come 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})" />
2
  • Your code should work fine if you take the clientId of the regularexpressionvalidator and check IsValid like you do. 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 :-( Commented Dec 28, 2010 at 14:46

2 Answers 2

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})" />
Sign up to request clarification or add additional context in comments.

2 Comments

I doubt you can access isValid property like you have in your original question. If you have single validator on the page then there was no need for validationgroup you could have done -- if(Page_ClientValidate()) { // Perform server side lookup in DB for whether the e-mail exists. }
@gbs: You're right about the single validator case - however I do have multiple validators on the page, I just simplified it for the question :-)
1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.