我想检查用户输入是否是JavaScript的电子邮件地址,然后将其发送到服务器或试图发送电子邮件,以防止最基本的误解。
当前回答
如果你把你的常规表达式定义为一个字符串,那么所有的背影都必须逃脱,所以而不是“\w”你应该有“\w”。
否则,将其定义为常规表达式:
var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
其他回答
在您的验证函数中使用此代码:
var emailID = document.forms["formName"]["form element id"].value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter correct email ID")
return false;
}
您可以使用 jQuery. 内部规则定义:
eMailId: {
required: true,
email: true
}
如果你想要一個人可以閱讀和維持的東西,我會推薦Masala Parser(我是它的創造者之一)。
import {C,Streams} from '@masala/parser'
const illegalCharset = ' @\u00A0\n\t';
const extendedIllegalCharset = illegalCharset + '.';
// Assume 'nicolas@internal.masala.co.uk'
export function simpleEmail() {
return C.charNotIn(illegalCharset).rep() // 'nicolas'
.then(C.char('@'))
.then(subDns()) //'internal.masala.co.'
.then(C.charNotIn(extendedIllegalCharset).rep()) //'uk'
.eos(); // Must be end of the char stream
}
// x@internal.masala.co.uk => extract 'internal.masala.co.'
function subDns() {
return C.charNotIn(extendedIllegalCharset).rep().then(C.char('.')).rep()
}
function validateEmail(email:string) {
console.log(email + ': ' + (simpleEmail().parse(Streams.ofString(email)).isAccepted()));
}
validateEmail('nicolas@internal.masala.co.uk'); // True
validateEmail('nz@co.'); // False, trailing "."
如果你想接受最终丑陋的电子邮件版本,你可以在第一部分添加引用:
function inQuote() {
return C.char('"')
.then(C.notChar('"').rep())
.then(C.char('"'))
}
function allEmail() {
return inQuote().or(C.charNotIn(illegalCharset))
.rep() // repeat (inQuote or anyCharacter)
.then(C.char('@'))
.then(subDns())
.then(C.charNotIn(extendedIllegalCharset).rep())
.eos() // Must be end of the character stream
// Create a structure
.map(function (characters) { return ({ email: characters.join('') }); });
}
“Nicolas”“love-quotes”@masala.co.uk”是正式有效的,但应该在您的系统中吗?
至少与马萨拉,你给自己一个机会来理解它。
這是從 http://codesnippets.joyent.com/posts/show/1917 偷走的
email = $('email');
filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) {
// Yay! valid
return true;
}
else
{return false;}
如果你把你的常规表达式定义为一个字符串,那么所有的背影都必须逃脱,所以而不是“\w”你应该有“\w”。
否则,将其定义为常规表达式:
var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
\b[a-z][\w\d_\.]+@\w+\.[a-z]{2}[a-z]?\.?[a-z]{,2}\s
它允许:
abcxyz123@qwert.com
abc123xyz@asdf.co.in
abc1_xyz1@gmail1.com
abc.xyz@gmail.com.in
推荐文章
- 使伸缩项目正确浮动
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 形式内联内的形式水平在twitter bootstrap?
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- 自定义元素在HTML5中有效吗?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?