1

Is there a way to do input validation (either in terms of offering a warning or, even better, simply blocking the characters) in an HTML text box using only CSS—no JavaScript? I want to do this because:

  1. Some input validation ought to happen on client side
  2. JavaScript can be disabled (I myself run NoScript)
  3. I always consider it a matter of civility to do things in CSS before JavaScript if possible

I'm thinking especially of CSS3 because of its impressive capabilities.

3
  • 1
    CSS is not meant for this kind of stuff. In fact you should never, ever touch any user input with CSS. You can make input controls and validation controls look flashy, but that's about it. CSS makes things look flashy, not change how things work. Remember that the second S stands for Style. Commented Nov 25, 2011 at 3:26
  • @BoltClock You point stands, perhaps, for prevention. But what about styling the control to reflect bad input, such as turning it red? CSS3 has support for events; if it is not outside of the intension of CSS to turn an element a different color on hover, it does not seem inconceivable that it could be used to change the style of the control on bad input. Commented Nov 25, 2011 at 3:54
  • Yes, that's absolutely fine. The answers below will do great assuming you're able to use HTML5. Commented Nov 25, 2011 at 3:56

3 Answers 3

4

You can do it somewhat with HTML5's new input types

See: http://www.456bereastreet.com/archive/201004/html5_input_types/

Sign up to request clarification or add additional context in comments.

Comments

3

Using HTML5, kind of yes. Link: http://thereforei.am/2011/07/01/css-selectors-for-html5-input-validation/

1 Comment

Note that CSS isn't actually involved with validating the input, all it does is change how those prompts look. It's HTML5 that's doing most of the heavy-lifting on the client side.
1

As others writes: Yes, with HTML5. BUT HTML5 is NOT fully supported by most browsers. So don't use this approach yet.

You can't do it with CSS, you have to use JavaScript until HTML5 is fully supported. But ALWAYS remember to validate server-side as well via ASP(.NET), PHP and such.. Doing both Client-side and Server-side will ensure the safety of your site even if JS are disabled :)

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.