input[type="file"] check existence of file attachment with css

It's not a JS question, just looking for a clear css solution if it's possible.

For a radio button or a checkbox we can use the :checked pseudo class:

input{
  % styles %
}
input:checked{
  % another styles %
}

Are there tricks for checking if a file attachment exists with CSS?


(updated, due to misunderstanding the question)

It is possible to check if a file was attached using the attribute required .

See: http://jsfiddle.net/1ua59jt1/1/

HTML:

<input id="file" type="file" name="fileselect" required="required" />

CSS:

#file:valid { color: green; }
#file:invalid { color: red; }

But you can never really validate, if a "correct" file was selected this way. You can only check if or if not a file was selected at all.


// completion update:

The required attribute is HTML5. Please be aware that not all browsers support this: http://www.w3schools.com/tags/att_input_required.asp

That means the only trusted way for client side validation is using javascript. If you want to do so, I recommend using the novalidate attribute on your form to disable the browsers default validation (http://www.w3schools.com/tags/att_form_novalidate.asp).

But always use server-side validation , too. You can never make sure the user has enabled jaavscript or hasn't manipulated javascript. So the best way is always to validate values on the side you have fully control over: the server, ie your action the form sends its data to.

链接地址: http://www.djcxy.com/p/28414.html

上一篇: 任务并行不稳定,有时使用100%的CPU

下一篇: 输入[type =“file”]用css检查是否存在文件附件