首页 > 解决方案 > How to validate a phone number doesn't contain any other characters including - and . using javascript in a html page, shouldnt be less than 10 digits

问题描述

This is in Adobe campaign classic.

type="text" maxlength="10" pattern="[0-9]{10}"

It is allowing only 10 digits, but accepting - and . I want the user to enter only 10 not less or more and accept only digits. Please help.

标签: javascripthtmladobeadobe-campaign-classic

解决方案


\D您可以做的是在用户输入后替换非数字字符( ):

[...document.querySelectorAll('input[type="tel"]')].forEach(i =>
  i.addEventListener('input', () => i.value = i.value.replace(/\D/g, ''))
);
<input type="tel" maxlength="10" pattern="\d{10}" />


推荐阅读