欧芹 + 无法验证全角字符的电子邮件地址
Parsley + Unable to validate Email address For Full Width Character
尝试在全角字符欧芹的电子邮件验证检查中写入:-
JS代码:-
window.Parsley.addValidator('validateFullWidthCharacters', {
validateString: function(_value) {
regex = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/;
if (regex.test(_value)) {
return false;
} else {
return true;
}
},
messages: {
en: 'Please enter a valid email address.'
}
});
工作正常并通过给出错误消息验证电子邮件
rahul@mail.com
但如果我将电子邮件更改为以下一个,则不起作用
rahul@mail.com
<input placeholder="e.g. mail@example.com" class="form-control parsley-success" id="user_email" data-parsley-required="" data-parsley-type="email" data-parsley-validate-full-width-characters="true" type="email" value="" name="user[email]" data-parsley-id="17">
请提出解决此问题的建议。
在 运行 验证电子邮件字符串之前,尝试从字符串中删除所有多余的空格。
像这样
tr = str.replace(/\s+/g, '');
你的正则表达式只匹配全角字符,你必须用普通字符测试你的值。
存在错误或可能是输入类型为 "email" 的 "expected behavior",如果在 @.
之前有效,它将全角字符转换为普通字符
您的解决方案是将 type="email" 更改为 type="text" 并使用另一种电子邮件模式并将其编辑为 return regex.test相反
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
新考试
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
/^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
regex.test('rahul@mail.com')
false
regex.test('rahul@mail.com')
false
regex.test('rahul@mail.com')
true
您的代码将变为:
window.Parsley.addValidator('validateFullWidthCharacters', {
validateString: function(_value) {
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm;
return regex.test(_value);
},
messages: {
en: 'Please enter a valid email address.'
}
});
html
<input placeholder="e.g. mail@example.com" class="form-control parsley-success" id="user_email" data-parsley-required="" data-parsley-type="email" data-parsley-validate-full-width-characters="true" type="text" value="" name="user[email]" data-parsley-id="17">
window.Parsley
.addValidator('validateFullWidthCharacters', {
requirementType: 'string',
validateString: function (value, requirement) {
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm;
return regex.test(value);
},
messages: {
en: 'Please enter a valid email address.'
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.7.2/parsley.min.js"></script>
</head>
<body>
<form data-parsley-validate>
<input type="text" name="email" placeholder="e.g. address@example.ext" data-parsley-validate-full-width-characters="true">
<button>Submit</button>
</form>
</body>
</html>
验证电子邮件的正则表达式是:
/^((([a-zA-Z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-Z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/
尝试在全角字符欧芹的电子邮件验证检查中写入:-
JS代码:-
window.Parsley.addValidator('validateFullWidthCharacters', {
validateString: function(_value) {
regex = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/;
if (regex.test(_value)) {
return false;
} else {
return true;
}
},
messages: {
en: 'Please enter a valid email address.'
}
});
工作正常并通过给出错误消息验证电子邮件
rahul@mail.com
但如果我将电子邮件更改为以下一个,则不起作用
rahul@mail.com
<input placeholder="e.g. mail@example.com" class="form-control parsley-success" id="user_email" data-parsley-required="" data-parsley-type="email" data-parsley-validate-full-width-characters="true" type="email" value="" name="user[email]" data-parsley-id="17">
请提出解决此问题的建议。
在 运行 验证电子邮件字符串之前,尝试从字符串中删除所有多余的空格。
像这样
tr = str.replace(/\s+/g, '');
你的正则表达式只匹配全角字符,你必须用普通字符测试你的值。
存在错误或可能是输入类型为 "email" 的 "expected behavior",如果在 @.
之前有效,它将全角字符转换为普通字符您的解决方案是将 type="email" 更改为 type="text" 并使用另一种电子邮件模式并将其编辑为 return regex.test相反
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
新考试
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
/^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm
regex.test('rahul@mail.com')
false
regex.test('rahul@mail.com')
false
regex.test('rahul@mail.com')
true
您的代码将变为:
window.Parsley.addValidator('validateFullWidthCharacters', {
validateString: function(_value) {
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm;
return regex.test(_value);
},
messages: {
en: 'Please enter a valid email address.'
}
});
html
<input placeholder="e.g. mail@example.com" class="form-control parsley-success" id="user_email" data-parsley-required="" data-parsley-type="email" data-parsley-validate-full-width-characters="true" type="text" value="" name="user[email]" data-parsley-id="17">
window.Parsley
.addValidator('validateFullWidthCharacters', {
requirementType: 'string',
validateString: function (value, requirement) {
regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/gm;
return regex.test(value);
},
messages: {
en: 'Please enter a valid email address.'
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.7.2/parsley.min.js"></script>
</head>
<body>
<form data-parsley-validate>
<input type="text" name="email" placeholder="e.g. address@example.ext" data-parsley-validate-full-width-characters="true">
<button>Submit</button>
</form>
</body>
</html>
验证电子邮件的正则表达式是:
/^((([a-zA-Z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-Z]|\d|[!#$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/