Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

javascript - 如何允许输入数字类型具有空格和逗号(How to allow input number type to have space and comma)

How can you allow input type="number" to accept something like 1, 2, 3, 55 rather than just regular number with decimal.(如何允许input type="number"接受类似于1、2、3、55之类的东西1, 2, 3, 55而不是仅接受带小数的常规number 。)

I tried using pattern but it didn't seems to work its magic as its still only accepting number and decimal.(我尝试使用模式,但是它似乎并没有发挥作用,因为它仍然只接受数字和小数。) Example: <input type="number" pattern="[0-9]+([\.,][0-9]+)?"> Desired: <input type="text" pattern="[0-9]+([\.,][0-9]+)?" value="1, 2, 3, 4"> <input type="number" pattern="[0-9]+([.,][0-9]+)?">   ask by MrNew translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should just use type=text and validated/format it using js.(您应该只使用type = text并使用js对其进行验证/格式化。)

$('input').on('change, keyup', function() { var currentInput = $(this).val(); var fixedInput = currentInput.replace(/[A-Za-z!@#$%^&*()]/g, ''); $(this).val(fixedInput); console.log(fixedInput); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...