你的正则表达式只匹配全角字符,你必须用普通字符测试你的值。
输入 type="email" 存在错误或“预期行为”,如果它在 @ 之前有效,它将全角字符转换为普通字符。
您的解决方案是将 type="email" 更改为 type="text" 并使用另一种电子邮件模式并编辑如果您返回 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>