有两个文本框用于电子邮件,另一个用于手机我使用了一个自定义验证控件,因此用户必须填写客户端的任何一个文本框我使用javascript
function ValidatePhoneEmail(source,args){
var tboxEmail = document.getElementById('<%= tboxEmail.ClientID %>');
var tboxPhone = document.getElementById('<%= tboxPhone.ClientID %>');
if (tboxEmail.value.trim() != '' || tboxPhone.value.trim() != '') {
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
如何使用jquery
实现相同的结果答案 0 :(得分:2)
完全相同的 validation plugin将在这些字段上使用"required"
,如下所示:
$(function() {
$("form").validate({
rules: {
<%= tboxEmail.UniqueID %>: "required",
<%= tboxPhone.UniqueID %>: "required"
}
});
});
You can see a full list of options here。如果您想添加自定义消息并验证它是有效的电子邮件地址,您可以这样做:
$(function() {
$("form").validate({
rules: {
<%= tboxEmail.UniqueID %>: { required: true, email: true },
<%= tboxPhone.UniqueID %>: "required"
},
messages: {
<%= tboxEmail.UniqueID %>: "Please enter a valid email address",
<%= tboxPhone.UniqueID %>: "Please enter a phone number"
}
});
});
答案 1 :(得分:0)
<script type="text/javascript">
$(document).ready(function() {
$("#aspnetForm").validate({
rules: {
<%=txtName.UniqueID %>: {
minlength: 2,
required: true
},
<%=txtEmail.UniqueID %>: {
required: true,
email:true
}
}, messages: {
<%=txtName.UniqueID %>:{
required: "* Required Field *",
minlength: "* Please enter atleast 2 characters *"
}
}
});
});
</script>
Name: <asp:TextBox ID="txtName" runat="server" /><br />
Email: <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />