我有一个表单和一个提交按钮。我想在这个提交按钮上添加一个带有jquery的条件。我不想只在用户输入有效的电子邮件地址时发布表单。
HTML code:
<form method="post" action="{% url sportdub.views.login %}">
<input type="text" name="email" value="" class="span8"/>
<input type="submit"></button>
</form>
Javascript代码:
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-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-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
我想要做的是在我的提交按钮上添加一个条件:
if( !isValidEmailAddress( emailaddress ) ) {
/* don't submit the form and raise an error */
} else {
/* submit the form */
}
关于如何实现这一点的任何想法?
答案 0 :(得分:3)
您需要在表单中添加onsubmit
事件处理程序,如果未通过验证,则需要return false
。
例如根据您的代码:
<form method="post" action="{% url sportdub.views.login %}" onsubmit="return validateForm();">
<input type="text" id="formEmail" name="email" value="" class="span8"/>
<input type="submit"></button>
</form>
JS中的:
function validateForm() {
if( !isValidEmailAddress( document.getElementById( 'formEmail' ).value ) ) {
// also before return you can add alert( 'your e-mail is invalid' );
// or display message in form, etc...
return false;
} else {
return true;
}
}
顺便说一句,我不建议使用内联JS属性,只需添加到所有元素并形成唯一的id属性,如果使用jQuery,则可以在 .submit() 中执行所有验证方法
答案 1 :(得分:2)
内联:
<form method="post" action="{% url sportdub.views.login %}"
onsubmit="return isValidEmailAddress(this.email.value)">
<input type="text" name="email" value="" class="span8"/>
<input type="submit"></button>
</form>
更好(普通的JS) - 注意我需要一个表格的ID:
window.onload=function() {
document.getElementById("form1").onsubmit=function() {
if (!isValidEmailAddress(this.email.value)) {
alert('Please enter a valid address');
return false; // cancel submit
}
return true; // allow submit
}
}
jQuery版本:
$(function() {
$("#form1").on("submit",function(e) {
if (!isValidEmailAddress($("#email").val())) {
alert('Please enter a valid address');
return false; // cancel submit
}
return true; // allow submit
});
});
最后两个假设
<form id="form1" method="post" action="{% url sportdub.views.login %}">
<input type="text" name="email" id="email" value="" class="span8"/>
<input type="submit"></button>
</form>
答案 2 :(得分:1)
使用返回在您的提交按钮中使用一个onclick调用...您的代码应该是,
<form method="post" action="{% url sportdub.views.login %}">
<input type="text" id="email" name="email" value="" class="span8"/>
<input type="submit" onclick = "return isValidEmailAddress();"></button>
</form>
function isValidEmailAddress() {
var emailAddress = $('#email').val();
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-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-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};