[英]multiple regexp in one input
hi i have one input type tel for mobile number and like use pattern for number format like this 938 119 1229
but when i use type tel i can use word in input i try use the two regex in one input but i don't know how:嗨,我有一种用于手机号码的输入类型 tel,并且喜欢使用这种数字格式的模式
938 119 1229
但是当我使用 tel 类型时,我可以在输入中使用单词,我尝试在一个输入中使用两个正则表达式,但我不知道如何:
function mobileNumInput(input){
var regex = /[^0-9]/g; //for digit only
input.value=input.value.replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3'); //for space between numbers
input.value=input.value.replace(regex)
}
and html:和 html:
<input type="tel" name="phone" placeholder="912 000 0000" maxlength="12" min="0" max="9" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" onkeyup="mobileNumInput(this)" autofocus>
this is my placeholder: enter image description here这是我的占位符:在此处输入图像描述
i want this format: enter image description here我想要这种格式:在此处输入图像描述
But I don't want to use word like this: enter image description here sorry my english is bad但我不想用这样的词:在此处输入图片描述对不起我的英语不好
I'm guessing that you want to do the following:我猜你想要执行以下操作:
'(111)-174-1234'
to '1111741234'
'(111)-174-1234'
转换为'1111741234'
'1111741234'
to '111 174 1234'
'1111741234'
转换为'111 174 1234'
If my assumptions about the purpose of this code are true, your code had two mistakes:如果我对这段代码用途的假设是正确的,那么您的代码有两个错误:
'(111)-174-1234'
having the regex (#2) running the following replacement: .replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3')
'(111)-174-1234'
具有正则表达式 (#2) 运行以下替换: .replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3')
'(111)-174-1234'
as containing any matches to /^(\d{3})(\d{3})(\d+)/
, so no replacements are made.'(111)-174-1234'
识别为包含与/^(\d{3})(\d{3})(\d+)/
的任何匹配项,因此不会进行替换。 In other words, after line 3 of your code runs, input.value
has likely not changed.input.value
可能没有改变。.replace(regex)
instead of .replace(regex,'')
..replace(regex)
而不是.replace(regex,'')
。
String
method technicality: String.prototype.replace
accepts a regex and a string to replace it.String
方法的技术性: String.prototype.replace
接受一个正则表达式和一个字符串来替换它。 Leaving the second parameter empty is the same as setting the second parameter as undefined
.undefined
相同。"Hello world".replace(/l/g)
is the same as "Hello world".replace(/l/g,undefined)
."Hello world".replace(/l/g)
与"Hello world".replace(/l/g,undefined)
相同。 The result of both of these snippets is "Heundefinedundefinedo world"
."Heundefinedundefinedo world"
。 You can gain the desired behavior by using "Hello world".replace(/l/g,'')
instead."Hello world".replace(/l/g,'')
来获得所需的行为。 This will return "Heo world"
."Heo world"
。 I put my fixes into a revised version of your code:我将我的修复程序放入您的代码的修订版本中:
function mobileNumInput(input){
var regex = /[^0-9]/g; //for digit only
input.value=input.value.replace(regex, ''); // Step #1 (remove non-digits)
input.value=input.value.replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3'); //Step #2 (add spaces to valid phone number)
}
Here is a slightly further modified version of your code with one test:这是您的代码的稍微进一步修改的版本,其中包含一个测试:
function mobileNumInput(input){
input.value=input.value
.replace(/[^0-9]/g, '')//Remove all non-digits
.replace(/^(\d{3})(\d{3})(\d+)/, '$1 $2 $3'); //Add spaces to the only-digit number
}
function test(){
var testInputElement=document.createElement("input");
testInputElement.value='(123)-[456} - 7890';//Horribly formatted phone number
mobileNumInput(testInputElement);
console.log(testInputElement.value);
}
test();
//123 456 7890
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.