简体   繁体   English

使用纯 JavaScript 屏蔽名字和姓氏

[英]Mask First & Last Name using plain JavaScript

I found this answer for masking a US phone number and it works fantastically:我找到了这个屏蔽美国电话号码的答案,它的效果非常好:

 document.getElementById('phone').addEventListener('input', function (e) { var x = e.target.value.replace(/\\D/g, '').match(/(\\d{0,3})(\\d{0,3})(\\d{0,4})/); e.target.value = !x[2] ? x[1] : '(' + x[1] + ') ' + x[2] + (x[3] ? '-' + x[3] : ''); });
 <input type="text" id="phone" placeholder="(555) 555-5555"/>

Now what I'm trying to do is to modify it to mask an input for first and last name where it will accept two strings of any length made up of only upper/lower alpha characters.现在我要做的是修改它以屏蔽名字和姓氏的输入,它会接受两个任意长度的字符串,这些字符串仅由上/下字母字符组成。

Javascript is not my strong suit and regex looks like matrix code to me, so I'm really hoping someone can put me in the right direction. Javascript 不是我的强项,正则表达式对我来说就像矩阵代码,所以我真的希望有人能把我放在正确的方向上。

This Regex matches a String with a first and last name separated by a white space.这个正则表达式匹配一个字符串,名字和姓氏用空格分隔。 fe "John Doe" fe“约翰·多伊”

([A-Za-z]* [A-Za-z]*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM