HTML5 input type=number value is empty in Webkit if has spaces or non-numeric characters?

后端 未结 5 1774
温柔的废话
温柔的废话 2020-12-01 15:39

This is strange behavior to me but on Webkit browsers (Chrome/Safari, not Firefox) if I include a space in a string of numbers in an

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 16:15

    My hack for this problem includes the following (i use jQuery validators):

        $(document).on('keyup', '[type="number"]', function () {
            if (this.validity.badInput) {
                $(this).attr('data-badinput', true);
            }
        });
    

    Later in validator method i do this:

        $.validator.addMethod('isInteger', function (value, element, parameterValue) {
            if ($(element).attr('data-badinput')) {
                //We know nasty browser always clears incorrect input, so empty string will look OK next time
                $(element).removeAttr('data-badinput');
                return false;
            }
            return !value || /^-?\d+$/.test(value);
        });
    

提交回复
热议问题