Firefox4: 有没有一种方法可以删除所需表单输入中的红色边框?

当需要在表单字段中定义时,Firefox 4会自动显示该元素的红色边框,甚至在用户点击提交按钮之前。

<input type="text" name="example" value="This is an example" required />

我认为这是令人不安的用户,因为他/她没有犯错误在开始。

我想隐藏初始状态的红色边框,但是如果有一个字段被标记为必需的,当用户点击发送按钮时显示它。

我看了 :required:invalid从新的伪选择器,但更改是为 之前和之后的验证

有没有办法在用户提交表单之前禁用红色边框,并在缺少字段时显示它?

83229 次浏览

This worked well for me:

input:invalid {
-moz-box-shadow: none;
}

This was a little tricky but I've set up this exmaple: http://jsfiddle.net/c5aTe/ which is working for me. Basically the trick seems to be getting around having placeholder text which is invalid. Otherwise you should be able do this:

input:required {
box-shadow:none;
}
input:invalid {
box-shadow:0 0 3px red;
}

or something similar...

BUT since FF4 decides to validate your placeholder text (no idea why...) the solution in the fiddle (little hacky - uses !important) is required.

Hope that helps!

EDIT

Doh!! - I feel well silly. I've updated my fiddle: http://jsfiddle.net/c5aTe/2/ - you can use the :focus pseudo class to keep the element styled as if valid while the user is typing. This will still highlight in red if the content is invalid when the item loses focus but I think there is only so much you can do with the designed behaviour...

HTH :)


EDIT after acceptance:

Summary of examples at OP's request (note the first two are only designed for FF4 - not Chrome)

  1. Fix for FF validating your place holder text: http://jsfiddle.net/c5aTe/
  2. Fix for FF validating as you type: http://jsfiddle.net/c5aTe/2
  3. JS solution toggling styles/validation: http://jsfiddle.net/c5aTe/4

Here is a very easy solution that worked for me. I basically changed the ugly red into a very nice blue, which is the standard color for non-required fields, and a web convention:

:required {
border-color: rgba(82, 168, 236, 0.8);
}

As of Firefox 26, the actual CSS used to identify invalid required fields is as follows (comes from forms.css):

:not(output):-moz-ui-invalid {
box-shadow: 0 0 1.5px 1px red;
}

To replicate in other browsers, I use:

input:invalid {
box-shadow: 0 0 1.5px 1px red;
}

I played around with the pixel settings but I never would have guessed the 1.5px without looking at moz source.

To disable it, you can use:

input:invalid {
box-shadow: none;
}

Please try this,

$("form").attr("novalidate",true);

for your form in your global .js file or in header section.

Try:

document.getElementById('myform').reset();

A way I found to at least mostly fix the issue is:

<input type="text" name="example" value="This is an example" onInput="this.required = true;" />

That way, the input field starts out with that nice blue outline, but once the user enters a character it's set to required (so if you enter a character then backspace, the red border is there). In this case it is possible for a user to skip the input, so make sure to put backend validation in place if you do this.