Change Text Box Color using Required Field Validator. No Extender Controls Please

前端 未结 16 2172
广开言路
广开言路 2020-11-28 07:07

I need to change color of TextBox whenever its required field validator is fired on Clicking the Submit button

16条回答
  •  醉话见心
    2020-11-28 07:51

    Another possibility... this code gives a red border (or whatever you put inside the CSS class) to the control to validate (works for dropdownlists and textbox, but can be extended for buttons etc...)

    First of, I make use of a CustomValidator instead of a RequiredFieldValidator, because then you can use the ClientValidationFunction of the CustomValidator to change the CSS of the control to validate.

    For example: change the border of a textbox MyTextBox when a user forgot to fill it in. The CustomValidator for the MyTextBox control would look like this:

    
         
    

    Or it could also work for a dropdownlist in which a selection is required. The CustomValidator would look the same as above, but with the ControlToValidate pointing to the dropdownlist.

    For the client-side script, make use of JQuery. The ValidateInput method would look like this:

        
    

    The “validation” class is a CSS class that contains the markup when the validator is fired. It could look like this:

    .validation { border: solid 2px red; }
    

    PS: to make the border color work for the dropdown list in IE, add the following meta tag to the page's heading: .

    [*]This is the same as the “InitialValue” of a RequiredFieldValidator. This is the item that is selected as default when the user hasn’t selected anything yet.​

提交回复
热议问题