0

I am trying to apply CssClass when validator is false, it worked for me in the past and now I don't know why it doesn't work.

Textbox:

<asp:TextBox ID="txtPass"
    AutoPostBack="false" 
    ClientIDMode="Static" 
    runat="server"
    placeholder="Password (8 - 16 digits)" 
    CssClass="Text" 
    type="password" 
    ValidationGroup="check">
</asp:TextBox>

Validators attached to this textbox:

<asp:RegularExpressionValidator ID="revPass"
    runat="server"
    Display="None" 
    ControlToValidate="txtPass"
    EnableClientScript="false"
    ValidationExpression="[a-zA-Z0-9]{8,16}"
    ValidationGroup="check">
</asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="rfvPass"
    runat="server"
    EnableClientScript="false"
    ControlToValidate="txtPass"
    Display="None"
    ValidationGroup="check">
</asp:RequiredFieldValidator>

<asp:CompareValidator ID="cvPasswords"
   runat="server"
   ErrorMessage="Passwords do not match!"
   EnableClientScript="false"
   CssClass="error"
   ControlToCompare="txtPass"
   ControlToValidate="txtConPass"
   ValidationGroup="check">
</asp:CompareValidator>

But I have to say that only the last one works. The validators work, just not changing CssClass. EDIT: It is not even enteres the if statemant I checked it. there is the if statement: if (!(revPass.IsValid) || !rfvPass.IsValid) { txtPass.CssClass = "txtError"; }

D4NieLDev
  • 260
  • 1
  • 3
  • 14
  • Why not enabling client script? People want instant response on validation, nowadays. – Martin Braun Apr 25 '16 at 10:48
  • 1
    did you apply `CssClass="error"` Attribute on all Validators .? i can see this only in last validation so that's why the last one works – KanisXXX Apr 25 '16 at 10:53
  • @KanisXXX Yes, in the code I assigned for each textbox. – D4NieLDev Apr 25 '16 at 11:55
  • @modiX Is this the problem? because it works in other pages like that – D4NieLDev Apr 25 '16 at 11:57
  • @D4NieLDev No it's not, but if you enable client script it will validate on client and server. This will result in a faster response (since most have JS enabled), but it will require you to implement a solution for JavaScript, too. By default, ASP.NET will not add any class name, it will show a span-div, when not valid. That's why it does not work the way you want. – Martin Braun Apr 25 '16 at 12:03
  • @D4NieLDev just for confirmation.. you want to change css of your Validations ..?? in short changing CSS of Error messages, right.? – KanisXXX Apr 25 '16 at 12:18
  • I want that if the validator is false it will change his controltovalidate textbox cssclass – D4NieLDev Apr 25 '16 at 12:30
  • @KanisXXX look at my comment above – D4NieLDev Apr 25 '16 at 12:36
  • Your CompareValidator's "ControlToValidate" is "txtConPass". What control is that? – Boyd P Apr 25 '16 at 15:35

1 Answers1

0

If you want to Apply Css on your Validator's Error messages Then you have to set CssClass Attribute of all Validators..

UPDATE :Sorry about that, i misunderstood your Question. Did you try this On Code behind

protected void Button1_OnClick(object sender, EventArgs e) { if (Page.IsValid) { // code executed when validation are valid } else { txtPass.CssClass = "Your New Class"; txtConPass.CssClass = "Your New Class"; } }

KanisXXX
  • 757
  • 7
  • 19