function checkValidPattern(element, completeCheck, isBarred)
{
if (completeCheck) {
gInvalid = false;
ok(!element.validity.patternMismatch, "Element should not suffer from pattern mismatch");
ok(element.validity.valid, "Element should be valid");
ok(element.checkValidity(), "Element should be valid");
ok(!gInvalid, "Invalid event shouldn't have been thrown");
is(element.validationMessage, '', "Validation message should be the empty string");
if (element.type != 'radio' && element.type != 'checkbox') {
is(window.getComputedStyle(element).getPropertyValue('background-color'),
isBarred ? "rgb(0, 0, 0)" : "rgb(0, 255, 0)", "The pseudo-class is not correctly applied");
}
} else {
ok(!element.validity.patternMismatch, "Element should not suffer from pattern mismatch");
}
}
function checkInvalidPattern(element, completeCheck)
{
if (completeCheck) {
gInvalid = false;
ok(element.validity.patternMismatch, "Element should suffer from pattern mismatch");
ok(!element.validity.valid, "Element should not be valid");
ok(!element.checkValidity(), "Element should not be valid");
ok(gInvalid, "Invalid event should have been thrown");
is(element.validationMessage, "Please match the requested format.", "Validation message is not valid");
} else {
ok(element.validity.patternMismatch, "Element should suffer from pattern mismatch");
}
if (element.type != 'radio' && element.type != 'checkbox') {
is(window.getComputedStyle(element).getPropertyValue('background-color'), "rgb(255, 0, 0)", ":invalid pseudo-class should apply");
}
}
function checkSyntaxError(element)
{
ok(!element.validity.patternMismatch, "On SyntaxError, element should not suffer");
}
function checkPatternValidity(element)
{
element.pattern = "foo";
// Check for 'i' flag disabled. Should be case sensitive.
element.value = "Foo";
checkInvalidPattern(element);
// We can't check for the 'g' flag because we only test, we don't execute.
// We can't check for the 'm' flag because .value shouldn't contain line breaks.
// We need '\\\\' because '\\' will produce '\\' and we want to escape the '\'
// for the regexp.
element.pattern = "foo\\\\bar";
element.value = "foo\\bar";
checkValidPattern(element);
// We may want to escape the ' in the pattern, but this is a SyntaxError
// when unicode flag is set.
element.pattern = "foo\\'bar";
element.value = "foo'bar";
checkSyntaxError(element);
element.value = "baz";
checkSyntaxError(element);
// We should check the pattern attribute do not pollute |RegExp.lastParen|.
is(RegExp.lastParen, "", "RegExp.lastParen should be the empty string");
element.pattern = "(foo)";
element.value = "foo";
checkValidPattern(element);
is(RegExp.lastParen, "", "RegExp.lastParen should be the empty string");
// That may sound weird but the empty string is a valid pattern value.
element.pattern = "";
element.value = "";
checkValidPattern(element);
// Checking some complex patterns. As we are using js regexp mechanism, these
// tests doesn't aim to test the regexp mechanism.
element.pattern = "\\d{2}\\s\\d{2}\\s\\d{4}"
element.value = "01 01 2010"
checkValidPattern(element);
// If @title is specified, it should be added in the validation message.
if (element.type == 'email') {
element.pattern = "foo@bar.com"
element.value = "bar@foo.com";
} else if (element.type == 'url') {
element.pattern = "http://mozilla.com";
element.value = "http://mozilla.org";
} else {
element.pattern = "foo";
element.value = "bar";
}
element.title = "this is an explanation of the regexp";
is(element.validationMessage, "Please match the requested format: " + element.title + ".", "Validation message is not valid");
element.title = "";
is(element.validationMessage, "Please match the requested format.", "Validation message is not valid");
// |validTypes| are the types which accept @pattern
// and |invalidTypes| are the ones which do not accept it. var validTypes = Array('text', 'password', 'search', 'tel', 'email', 'url'); var barredTypes = Array('hidden', 'reset', 'button'); var invalidTypes = Array('checkbox', 'radio', 'file', 'number', 'range', 'date', 'time', 'color', 'submit', 'image', 'month', 'week', 'datetime-local');
for (type of validTypes) { input.type = type;
completeValidityCheck(input, false);
checkPatternValidity(input);
}
for (type of barredTypes) { input.type = type;
completeValidityCheck(input, true, true);
}
for (type of invalidTypes) { input.type = type;
completeValidityCheck(input, true);
}
</script>
</pre>
</body>
</html>
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.38 Sekunden
(vorverarbeitet am 2026-05-01)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.