Greetings,
I have 2 textboxs, 2 checkboxs and 2 labels.
first textbox, checkbox, and label related to each other and the same for the rest.
textbox should accept valid phone number based on jquery validation plugin and when the chackbox check the validation rule would change and in both option the error message would disply inside the label.
I have no problem to implement that for one text box but when I add second one will have a problem and only the validation will happen for the second one only.
please look at my code and advice.
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
var RegularExpression;
var USAPhone = /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/;
var InterPhone = /^\d{9,12}$/;
var errmsg;
function ValidPhoneHome(sender) {
if (sender.checked == true) {
RegularExpression = InterPhone;
errmsg = "Enter 9 to 12 numbers as international number";
}
else {
RegularExpression = USAPhone;
errmsg = "Enter a valid number";
}
jQuery.validator.addMethod("phonehome", function(value, element) {
return this.optional(element) || RegularExpression.test(value);
}, errmsg);
}
function ValidMobileHome(sender) {
if (sender.checked == true) {
RegularExpression = InterPhone;
errmsg = "Enter 9 to 12 numbers as international number";
}
else {
RegularExpression = USAPhone;
errmsg = "Enter a valid number";
}
jQuery.validator.addMethod("mobilephone", function(value, element) {
return this.optional(element) || RegularExpression.test(value);
}, errmsg);
}
$(document).ready(function() {
ValidPhoneHome("#<%= chkIntphoneHome%>");
ValidMobileHome("#<%= chkIntMobileHome%>");
$("#aspnetForm").validate({
rules: {
"<%=txtHomePhone.UniqueID %>": {
phonehome: true
}
}
, errorLabelContainer: "#lblHomePhone",
rules: {
"<%=txtMobileHome.UniqueID %>": {
mobilephone: true
}
}
, errorLabelContainer: "#lblMobileHome"
})
</script>
<asp:CheckBox ID="chkIntphoneHome" runat="server" Text="Internation Code" onclick="ValidPhoneHome(this)" >
<asp:TextBox ID="txtHomePhone" runat="server" ></asp:TextBox>
<label id="lblHomePhone"></label>
<asp:CheckBox ID="chkIntMobileHome" runat="server" Text="Internation Code" onclick="ValidMobileHome(this)" />
<asp:TextBox ID="txtMobileHome" runat="server"></asp:TextBox>
<label id="lblMobileHome"></label>