Regex:排除字符组合并限制 Regex / jQuery 中的某些字符(部分工作)
Regex: Excluding combinations of characters and limiting certain characters in Regex / jQuery (partly working)
我正在尝试在 jQuery 中使用正则表达式验证电子邮件输入 - 仅针对以下条件:
我要允许的输入:
- 小写字母(多个)
- 位数(多个)
- 点/句点(多个)
- 破折号(多个)
- @签名(只有一个)
我要阻止的输入:
- 字符串开头的点/句点、破折号、@符号
- 字符串末尾的点/句号、破折号、@符号
- 连续两个点/句点或两个破折号
- 连续一个点/句点和一个破折号(或一个破折号和一个点/句点)
到目前为止,我想到了以下内容,但我在排除两个符号的组合以及将 @ 符号限制为仅一个符号时遇到了问题。
有人可以告诉我如何正确执行此操作吗?
注:
我只是在寻找对上述标准和英文字母的基本验证(我在 jQuery 中涵盖的所有其他内容)。
我的正则表达式:
var val = $(this).val();
var checkEmail = /(?!^[@.-])(?!.*@@)(?!.*\.\.)(?!.*--)(?!.*[@.-]$)[0-9a-z@\.\-]/g;
if(val.match(checkEmail) {
console.log('input valid');
} else {
console.log('input invalid');
}
非常感谢,
汤姆
您可以使用下面的正则表达式来获得您的结果:
^(?!.+?(?:\.-|\.\.|--|-@|\.@|@\.).+?)[^.-@ ][a-z0-9_.-]+@[a-z0-9_.-]+(?<=[^.-@ ])$
上面正则表达式的解释:
^, $
- Represents start and end of test string resp.
(?!.+?(?:\.-|\.\.|--|-@|\.@|@\.).+?)
- Represents negative lookahead not matching the test string if it contains consecutive .-
, ..
, --
, -@
, .@
or @.
.
[^.-@ ]
- Represents negative match if string starts with any of .-@
.
[a-z0-9_.-]+@[a-z0-9_.-]+
- Represents the character set where the String contains anything in the characters mentioned above with only one @
sign.
(?<=[^.-@ ])
- Represents positive lookbehind which matches any test string not ending with .-@
.
你可以找到上面正则表达式的演示here.
在 JAVASCRIPT(JQUERY) 中的实现:
var val = "hello@gmail.com";
var checkEmail = /^(?!.+?(?:\.-|\.\.|--|-@|\.@|@\.).+?)[^.-@ ][a-z0-9_.-]+@[a-z0-9_.-]+(?<=[^.-@ ])$/gm;
if (val.match(checkEmail)) {
console.log('input valid');
} else {
console.log('input invalid');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
我正在尝试在 jQuery 中使用正则表达式验证电子邮件输入 - 仅针对以下条件:
我要允许的输入:
- 小写字母(多个)
- 位数(多个)
- 点/句点(多个)
- 破折号(多个)
- @签名(只有一个)
我要阻止的输入:
- 字符串开头的点/句点、破折号、@符号
- 字符串末尾的点/句号、破折号、@符号
- 连续两个点/句点或两个破折号
- 连续一个点/句点和一个破折号(或一个破折号和一个点/句点)
到目前为止,我想到了以下内容,但我在排除两个符号的组合以及将 @ 符号限制为仅一个符号时遇到了问题。
有人可以告诉我如何正确执行此操作吗?
注:
我只是在寻找对上述标准和英文字母的基本验证(我在 jQuery 中涵盖的所有其他内容)。
我的正则表达式:
var val = $(this).val();
var checkEmail = /(?!^[@.-])(?!.*@@)(?!.*\.\.)(?!.*--)(?!.*[@.-]$)[0-9a-z@\.\-]/g;
if(val.match(checkEmail) {
console.log('input valid');
} else {
console.log('input invalid');
}
非常感谢,
汤姆
您可以使用下面的正则表达式来获得您的结果:
^(?!.+?(?:\.-|\.\.|--|-@|\.@|@\.).+?)[^.-@ ][a-z0-9_.-]+@[a-z0-9_.-]+(?<=[^.-@ ])$
上面正则表达式的解释:
^, $
- Represents start and end of test string resp.
(?!.+?(?:\.-|\.\.|--|-@|\.@|@\.).+?)
- Represents negative lookahead not matching the test string if it contains consecutive.-
,..
,--
,-@
,.@
or@.
.
[^.-@ ]
- Represents negative match if string starts with any of.-@
.
[a-z0-9_.-]+@[a-z0-9_.-]+
- Represents the character set where the String contains anything in the characters mentioned above with only one@
sign.
(?<=[^.-@ ])
- Represents positive lookbehind which matches any test string not ending with.-@
.
你可以找到上面正则表达式的演示here.
在 JAVASCRIPT(JQUERY) 中的实现:
var val = "hello@gmail.com";
var checkEmail = /^(?!.+?(?:\.-|\.\.|--|-@|\.@|@\.).+?)[^.-@ ][a-z0-9_.-]+@[a-z0-9_.-]+(?<=[^.-@ ])$/gm;
if (val.match(checkEmail)) {
console.log('input valid');
} else {
console.log('input invalid');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>