我有一个联系表。提交时使用AJAX加载“/forms/contact_received.php”给我发电子邮件&将用户的联系信息添加到数据库。类似于数据库中10%条目(及其相关电子邮件)的东西没有电子邮件地址。jQuery验证电子邮件允许空白不知何故
我似乎无法复制这个问题。有一段时间我有表单寄给我的用户代理,希望他们都有相同的浏览器,但它是一个横截面,一些Safari浏览器,一些Mozilla,一些IE浏览器,一些手机,一些桌面。此外,空白的电子邮件提交也不是恶意的,这些都是来自实际人类的合法问题。
我的剧本看起来是这样的:
<script type="text/javascript">
function checkForm() {
hideAllErrors();
var formIsValid =
showErrorAndFocusIf('FieldData0', isEmpty, 'nameError')
&& showErrorAndFocusIf('FieldData2', isEmpty, 'emailError')
&& showErrorAndFocusIf('FieldData2', isAnInvalidEmail, 'emailError2')
&& showErrorAndFocusIf('FieldData1', isEmpty, 'questionError');
return formIsValid;
}
function showErrorAndFocusIf(fieldId, predicate, errorId) {
var field = document.getElementById(fieldId);
if (predicate(field)) {
document.getElementById(errorId).style.display = 'inline';
if (field.select) {
field.select();
}
field.focus();
return false;
}
return true;
}
function isEmpty(field) {
return field.value == '';
}
function isAnInvalidEmail(field) {
var email = field.value;
var ok = "1234567890qwertyuiop[][email protected]_QWERTYUIOPASDFGHJKLZXCVBNM";
for(i = 0; i < email.length; i++){
if(ok.indexOf(email.charAt(i)) < 0) {
return true;
}
}
re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
return re.test(email) || !re_two.test(email);
}
function hideAllErrors() {
document.getElementById("nameError").style.display = "none"
document.getElementById("emailError").style.display = "none"
document.getElementById("emailError2").style.display = "none"
document.getElementById("questionError").style.display = "none"
}
</script>
和我的HTML看起来像这样:
<style>
.error{display:inline;margin:0;}
</style>
<form id="contact" name="contact" action="">
<b>Name</b>
<input class="valid" name="name" value="" maxlength="100" size="30" style="display:block;" type="text">
<b>Email</b> (will not be published)
<input class="error" name="email" value="" maxlength="100" size="30" style="display:block;" type="email">
<label class="error" generated="true" for="email">Please enter a valid email address</label>
<b>Question</b>
<textarea class="error" name="message" cols="50" rows="5" style="display:block;"></textarea>
<label class="error" generated="true" for="message">Please enter your message in the box above</label>
<input name="page" value="[URL]" type="hidden">
<input class="btn" value="Contact Us" name="Submit" type="submit">
</form>
什么可能是我的验证可能失败的一些可能的情况呢?就像我说的,验证对我来说很好,我无法弄清楚它将如何被绕过。
javascript在浏览器中禁用?!无论如何,验证也应该在服务器端完成 – 2014-11-14 21:43:46
我们假设很多 - 验证方法曾经被调用过(未显示),如果是的话,何时;您的电子邮件字段要么添加了一个匹配“FieldData2”的id,要么这是虚拟的HTML,它仍然*真*接近实时HTML;验证处理程序的结果不会被忽略;等 – 2014-11-14 21:47:12