Can we use jquery for client side validation of a custom validation control?
There are two text boxes for email and another for phone. I used one custom validation element to allow the user to fill in any text field for the client side. I have used javascript
ValidatePhoneEmail function (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;
}
}
how to achieve the same result using jquery
+2
a source to share
2 answers
The exact plugin equivalent is to use "required"
in these fields, for example:
$(function() {
$("form").validate({
rules: {
<%= tboxEmail.UniqueID %>: "required",
<%= tboxPhone.UniqueID %>: "required"
}
});
});
You can see a complete list of options here . If you would like to add a custom message and confirm that it is a valid email address, you can do it like this:
$(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"
}
});
});
+2
a source to share
<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 />
0
a source to share