Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则表达式校验手机号码居然出错? #182

Open
ly2011 opened this issue Sep 20, 2019 · 0 comments
Open

正则表达式校验手机号码居然出错? #182

ly2011 opened this issue Sep 20, 2019 · 0 comments
Labels

Comments

@ly2011
Copy link
Owner

ly2011 commented Sep 20, 2019

问题:今日遇到一个神奇的问题,用户那边反馈在input框粘贴手机号码,校验失败,原因是粘贴时带有特殊编码
解决方案:过滤掉不是整型的字符

function getTelPhone (phone) {
	if (typeof phone !== 'string') return ''
	var phoneReg = /[0-9]/
	var phoneNum = ''
	for (i = 0; i < phone.length; i++) {
		if (phoneReg.test(phone.charAt(i))) {
			phoneNum += phone.charAt(i)
		}
	}
	return phoneNum
}

var phone = '‭13712312312‬' // 此处的phone是有特殊编码的,长度为13
console.log(getTelPhone(phone), getTelPhone(phone).length)
@ly2011 ly2011 added the debug label Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant