I'm trying to validate a name with regex, the regex stops the user from entering 2 spaces or dots in a row.
我试图用regex验证一个名称,regex阻止用户在一行中输入两个空格或点。
This is my code:
这是我的代码:
function test(input) {
var regex = /^[A-Za-z]+\.{0,1}\s{0,1}$/;
input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
At the moment I want to enter a letter, I cant I don't know why, I hope you guys can help.
现在我想写一封信,我不能我不知道为什么,我希望你们能帮忙。
EDIT: Name only contains letters, not numbers.
编辑:名字只包含字母,不包含数字。
6 个解决方案
#1
1
The following regex should work for you: /[^a-zA-Z. ]|\.(?=\.)|\s(?=\s)/g
以下正则表达式应该为你工作:/[^ a-zA-Z。(? = \]| \。)| \ s(? = \ s)/ g
-
[^a-zA-Z. ]+
Limits all allowed characters to a-z, A-Z, dots, or spaces - [^ a-zA-Z。+将所有允许的字符限制为a-z、a-z、点或空格
-
\.(?=\.)
Limits it to only allow one dot in a row - (? = \ \。)限制它只允许一行中有一个点
-
\s(?=\s)
Limits it to only one space in a row - (?=\s)把它限制在一行中只有一个空间。
Each of these are separated by | (the or condition)
每一个都被| (or条件)分开
function test(input) {
var regex = /[^a-zA-Z. ]|\.(?=\.)|\s(?=\s)/g;
input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
#2
1
Your regex seems correct (it matches any string with no more than 1 space or 1 dot together), but your the logic in your function seems wrong.
您的regex似乎是正确的(它匹配任何不超过1空格或1点的字符串),但是您的函数中的逻辑似乎是错误的。
What it's doing: anytime the value changes, take the value and replace anything that matches that regex with an empty string, so any time you write a character its replaced (because it matches the regex).
它的作用是:当值发生变化时,取值并用空字符串替换与regex匹配的任何内容,这样每当您编写它所替换的字符时(因为它与regex匹配)。
I would go with one of this options:
我会选择其中一个选项:
a) show a message if the value doesn't match the regex
a)如果值与regex不匹配,则显示一条消息
b) change the regex to /.{2}| {2}/ and only replace the two dots or the two spaces, as Scath says in another answer
b)将regex更改为/。{2}|{2}/只替换两个点或两个空格,正如Scath在另一个答案中所说
c) maybe just using the input pattern attribute with your original regex is enough: https://www.w3schools.com/TAGS/att_input_pattern.asp
c)可能仅仅使用输入模式属性和原始regex就足够了:https://www.w3schools.com/TAGS/att_input_pattern.asp
Hope this helps!
希望这可以帮助!
#3
1
With this solution it deletes only the second space or second dot and the first ones will still remain:
有了这个解决方案,它只删除第二个空间或第二个点,第一个点仍然保留:
function test(input){
var regex=/\.(?=\.)|\s(?=\s)/;
input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
#4
1
To achieve expected result, use below option of slice
为了达到预期的效果,使用下面的切片选项
input.value.slice(-2) will return last two entered characters and if it is ".." or " "(double spaces) replace it with ''
slice(-2)将返回最后两个输入字符,如果是“..”或“”(双空格),则将其替换为“”
function test(input){
if(input.value.slice(-2)=='..' || input.value.slice(-2)==' '){
input.value = input.value.replace(input.value.slice(-2), "");
}
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
code sample - https://codepen.io/nagasai/pen/VXNOej
代码示例——https://codepen.io/nagasai/pen/VXNOej
Your regex clears any characters starts with [A-Z] or [a-z] due to expression ^[A-Za-z]
你的正则表达式清除任何字符开始[a - z]或[a - z]由于表达^[A-Za-z]
#5
1
This regex will not allow them to enter two periods or two spaces, when they enter the second of either it will clear.
这个regex将不允许它们进入两个周期或两个空间,当它们进入第二个周期或两个空间时,它们将被清除。
function test(input){
var regex=/\.{2}| {2}/;
input.value = input.value.replace(regex, "").replace(/\d+|/g, '').replace(/\s+/g, ' ').replace(/[^\w]/, '');
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
#6
1
That is because you match [A-Za-z]+
which will match one or more characters and everything after that is optional.
这是因为您匹配[A-Za-z]+,它将匹配一个或多个字符,之后的所有内容都是可选的。
Your regex would match.
你的正则表达式匹配。
-
[A-Za-z]+
Match one or more characters - [A-Za-z]+匹配一个或多个字符。
-
\.{0,1}
Match optional dot (0 or 1 times) - \。{0,1}匹配可选点(0或1次)
-
\s{0,1}
match optional whitespace character (0 or 1 times) - \s{0,1}匹配可选空格字符(0或1次)
Which will match a
and then you will do the replace.
它将匹配a,然后进行替换。
To make something optional you could also use a question mark ?
要做一些可选的东西,你也可以用问号?
To only allow characters, a single whitespace or a single dot you could replace all that does not match those criteria:
为了只允许字符、一个空格或一个点,您可以替换所有不符合这些条件的字符:
((。)(? = \ 1)|(^ a-zA-Z。]
function test(input) {
var regex = /([. ])(?=\1)|[^a-zA-Z. ]/;
input.value = input.value.replace(regex, "");
}
<form id="" name="">
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
</form>
To match a dot followed by a dot or a white space followed by a white space you could capture a dot or a white space in a group and it will match if it is followed by what is captured in the group using a positive lookahead.
要匹配一个点后面跟着一个点或一个空格后面跟着一个空格,你可以在一个组中捕获一个点或一个空格,如果它后面跟着一个点或一个空格,那么它就会匹配,如果它后面跟着一个用正向前视捕获的组。
((。)(? = \ 1)
#1
1
The following regex should work for you: /[^a-zA-Z. ]|\.(?=\.)|\s(?=\s)/g
以下正则表达式应该为你工作:/[^ a-zA-Z。(? = \]| \。)| \ s(? = \ s)/ g
-
[^a-zA-Z. ]+
Limits all allowed characters to a-z, A-Z, dots, or spaces - [^ a-zA-Z。+将所有允许的字符限制为a-z、a-z、点或空格
-
\.(?=\.)
Limits it to only allow one dot in a row - (? = \ \。)限制它只允许一行中有一个点
-
\s(?=\s)
Limits it to only one space in a row - (?=\s)把它限制在一行中只有一个空间。
Each of these are separated by | (the or condition)
每一个都被| (or条件)分开
function test(input) {
var regex = /[^a-zA-Z. ]|\.(?=\.)|\s(?=\s)/g;
input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
#2
1
Your regex seems correct (it matches any string with no more than 1 space or 1 dot together), but your the logic in your function seems wrong.
您的regex似乎是正确的(它匹配任何不超过1空格或1点的字符串),但是您的函数中的逻辑似乎是错误的。
What it's doing: anytime the value changes, take the value and replace anything that matches that regex with an empty string, so any time you write a character its replaced (because it matches the regex).
它的作用是:当值发生变化时,取值并用空字符串替换与regex匹配的任何内容,这样每当您编写它所替换的字符时(因为它与regex匹配)。
I would go with one of this options:
我会选择其中一个选项:
a) show a message if the value doesn't match the regex
a)如果值与regex不匹配,则显示一条消息
b) change the regex to /.{2}| {2}/ and only replace the two dots or the two spaces, as Scath says in another answer
b)将regex更改为/。{2}|{2}/只替换两个点或两个空格,正如Scath在另一个答案中所说
c) maybe just using the input pattern attribute with your original regex is enough: https://www.w3schools.com/TAGS/att_input_pattern.asp
c)可能仅仅使用输入模式属性和原始regex就足够了:https://www.w3schools.com/TAGS/att_input_pattern.asp
Hope this helps!
希望这可以帮助!
#3
1
With this solution it deletes only the second space or second dot and the first ones will still remain:
有了这个解决方案,它只删除第二个空间或第二个点,第一个点仍然保留:
function test(input){
var regex=/\.(?=\.)|\s(?=\s)/;
input.value = input.value.replace(regex, "");
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
#4
1
To achieve expected result, use below option of slice
为了达到预期的效果,使用下面的切片选项
input.value.slice(-2) will return last two entered characters and if it is ".." or " "(double spaces) replace it with ''
slice(-2)将返回最后两个输入字符,如果是“..”或“”(双空格),则将其替换为“”
function test(input){
if(input.value.slice(-2)=='..' || input.value.slice(-2)==' '){
input.value = input.value.replace(input.value.slice(-2), "");
}
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
code sample - https://codepen.io/nagasai/pen/VXNOej
代码示例——https://codepen.io/nagasai/pen/VXNOej
Your regex clears any characters starts with [A-Z] or [a-z] due to expression ^[A-Za-z]
你的正则表达式清除任何字符开始[a - z]或[a - z]由于表达^[A-Za-z]
#5
1
This regex will not allow them to enter two periods or two spaces, when they enter the second of either it will clear.
这个regex将不允许它们进入两个周期或两个空间,当它们进入第二个周期或两个空间时,它们将被清除。
function test(input){
var regex=/\.{2}| {2}/;
input.value = input.value.replace(regex, "").replace(/\d+|/g, '').replace(/\s+/g, ' ').replace(/[^\w]/, '');
}
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
#6
1
That is because you match [A-Za-z]+
which will match one or more characters and everything after that is optional.
这是因为您匹配[A-Za-z]+,它将匹配一个或多个字符,之后的所有内容都是可选的。
Your regex would match.
你的正则表达式匹配。
-
[A-Za-z]+
Match one or more characters - [A-Za-z]+匹配一个或多个字符。
-
\.{0,1}
Match optional dot (0 or 1 times) - \。{0,1}匹配可选点(0或1次)
-
\s{0,1}
match optional whitespace character (0 or 1 times) - \s{0,1}匹配可选空格字符(0或1次)
Which will match a
and then you will do the replace.
它将匹配a,然后进行替换。
To make something optional you could also use a question mark ?
要做一些可选的东西,你也可以用问号?
To only allow characters, a single whitespace or a single dot you could replace all that does not match those criteria:
为了只允许字符、一个空格或一个点,您可以替换所有不符合这些条件的字符:
((。)(? = \ 1)|(^ a-zA-Z。]
function test(input) {
var regex = /([. ])(?=\1)|[^a-zA-Z. ]/;
input.value = input.value.replace(regex, "");
}
<form id="" name="">
<input id="txt_NomCandidato" onkeyup="test(this);" type="text" class="form-control" name="txt_Nom">
</form>
To match a dot followed by a dot or a white space followed by a white space you could capture a dot or a white space in a group and it will match if it is followed by what is captured in the group using a positive lookahead.
要匹配一个点后面跟着一个点或一个空格后面跟着一个空格,你可以在一个组中捕获一个点或一个空格,如果它后面跟着一个点或一个空格,那么它就会匹配,如果它后面跟着一个用正向前视捕获的组。
((。)(? = \ 1)