Liam
Liam

Reputation: 9855

.net Validation

Im working on a website built in .net and im having some trouble with a 'validation'

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
    ErrorMessage="<p>Invalid Phone Number!</p>"........

What this does is posts a message on the page with an inline style, what I need it to do is add a class to an input field instead, is this possible?

Upvotes: 1

Views: 180

Answers (3)

twDuke
twDuke

Reputation: 927

This might work.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
ErrorMessage="<p>Invalid Phone Number!</p>"........

<cc1:ValidatorCalloutExtender HighlightCssClass="your_css_class" runat="server" TargetControlID="RegularExpressionValidator2" Enabled="true"/>

Upvotes: 1

Kiley Naro
Kiley Naro

Reputation: 1769

I would recommend using a CustomValidator for this instead:

http://msdn.microsoft.com/en-us/library/9eee01cx(v=VS.100).aspx

<asp:CustomValidator id="CustomValidator1"
       ControlToValidate="Text1"
       ClientValidationFunction="ClientValidate"
       ErrorMessage="<p>Invalid Phone Number!</p>"
       runat="server"/>


<script language="javascript">
   function ClientValidate(source, arguments)
   {
      var regexValid = false; // perform regular expression validation here manually
      if (regexValid) {
         arguments.IsValid=true;
      }
      else {
         // add the class to the desired input field
         arguments.IsValid=false;
      }
   }
</script>

Upvotes: 2

El Ronnoco
El Ronnoco

Reputation: 11922

You may need to look at a CustomValidator instead and write your regex validation and desired failure action in your own client-side script.

Upvotes: 1

Related Questions