chromium/third_party/blink/web_tests/fast/forms/number/number-validity-typemismatch.html

<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>
description('This test aims to check for typeMismatch flag with type=number input fields');

var i = document.createElement('input');
i.type = 'number';

// TODO(tkent): check() doesn't make sense because the number type never be
// typeMismatch.
function check(value, disabled)
{
    i.value = value;
    i.disabled = !!disabled;
    var mismatch = i.validity.typeMismatch;
    var resultText = '"' + value + '" is ' + (mismatch ? 'an invalid' : 'a valid') + ' number' + (disabled ? ' when disabled.' : '.');
    if (!mismatch)
        testPassed(resultText);
    else
        testFailed(resultText);
}

function checkSanitization(value, expectedValue)
{
    i.value = value;
    if (i.validity.typeMismatch) {
        testFailed('"' + value + '" made typeMismatch true.');
    } else if (i.value != expectedValue) {
        testFailed('"' + value + '" was sanitized to "' + i.value + '". It should be ' + expectedValue);
    } else {
        testPassed('"' + value + '" was sanitized to "' + i.value + '".');
    }
}

// Valid values
check('0');
check('10');
check('01');
check('0.2');
check('.2');
check('-0');
check('-1.2');
check('1.2E10');
check('1.2E-10');
check('1.2E+10');
check('12345678901234567890123456789012345678901234567890');
check('0.12345678901234567890123456789012345678901234567890');

// Invalid values
checkSanitization('abc', '');
checkSanitization('0xff', '');

checkSanitization('+1', '');
checkSanitization(' 10', '');
checkSanitization('10 ', '');
checkSanitization('0.', '');
checkSanitization('1.', '');
checkSanitization('1,2', '');
checkSanitization('1E', '');
checkSanitization('NaN', '');
checkSanitization('nan', '');
checkSanitization('Inf', '');
checkSanitization('inf', '');
checkSanitization('Infinity', '');
checkSanitization('infinity', '');

// Assume empty string as valid.
check('');

// Huge exponent.
checkSanitization('1.2E65535', '');

checkSanitization('1.2e10', '1.2e10');

// Disabled
check('invalid', true);
</script>
</body>
</html>