javascript - 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.
解决方案
\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}" />
推荐阅读
- swift - 如何像第一次在 Swift 中一样重新加载视图控制器?
- solr - Solr : Suggester 字典构建创建巨大的临时文件
- azure - 部署在 azure 上的 Bot 突然变得无响应
- c# - 使用 DropBox SDK C# 使用 UploadSessionStartAsync 覆盖现有文件
- ajax - 是否可以在 ASP.NET Core 2.1 Razor 页面中刷新视图组件?
- ios - 我们如何在单例中使用 Alamofire NetworkReachabilityManager?
- jenkins - 基于提交消息条件构建或自上次成功后更改
- oracle - 如何将数据库中的特定数据填充到列表项中?
- java - Spring批处理jobParameters在java配置中不起作用
- java - 更改 Apache Tomcat 中的站点级别配置