Description:-
In this
example we explain that how to validate checkbox is checked in a sequence order
or not. Or validate checkbox selection in sequence order using jquery.or jquery getting sequence number from selected checkbox on checkbox
list.or checkbox validation in asp.net using javascript or jquery.
I have faced real problem in my application like in my application one
form contain four checkbox year1, year2, year3 and year4. In which condition is
that user can select checkbox of the year must be in sequence order like
1,2,3,4. If user select year2 checkbox and year1 checkbox is not checked then
alert message will be display like please select year checkbox in sequence
order.
So how to achieve this at client side using JavaScript or jquery.
Code:-
function
ValidatechkSequence(id) {
var chkdefault = "1";
var chk1 = "2";
var chk2 = "3";
var chk3 = "4";
if (id.indexOf(chkdefault) > -1) {
if ($('#<%= chkYear1.ClientID %>').is(':checked')) {
__doPostBack('<%= chkYear1.ClientID %>', '');
}
else if ($('#<%=chkYear1.ClientID %>').attr('checked', false) && $('#<%= chkYear2.ClientID %>').is(':checked') || $('#<%= chkYear3.ClientID %>').is(':checked') || $('#<%= chkYear4.ClientID %>').is(':checked')) {
alert("Year Option must be
in sequence order");
return false;
}
else {
$('#<%=chkYear1.ClientID %>').attr('checked', false)
__doPostBack('<%= chkYear1.ClientID %>', '');
}
}
if (id.indexOf(chk1) > -1) {
if ($('#<%= chkYear1.ClientID %>').is(':checked') && $('#<%= chkYear2.ClientID %>').is(':checked')) {
__doPostBack('<%= chkYear2.ClientID %>', '');
}
else if ($('#<%= chkYear2.ClientID %>').is(':checked') && $('#<%=chkYear1.ClientID %>').attr('checked', false)) {
alert("Year Option must be
in sequence order");
return false;
}
else if ($('#<%=chkYear2.ClientID %>').attr('checked', false) && $('#<%= chkYear3.ClientID %>').is(':checked') || $('#<%= chkYear4.ClientID %>').is(':checked')) {
alert("Year Option must be
in sequence order");
return false;
}
else {
$('#<%=chkYear2.ClientID %>').attr('checked', false)
__doPostBack('<%= chkYear2.ClientID %>', '');
}
}
if (id.indexOf(chk2) > -1) {
if ($('#<%= chkYear1.ClientID %>').is(':checked') && $('#<%= chkYear2.ClientID %>').is(':checked') && $('#<%= chkYear3.ClientID %>').is(':checked')) {
__doPostBack('<%= chkYear3.ClientID %>', '');
}
else if ($('#<%= chkYear3.ClientID %>').is(':checked') && $('#<%=chkYear2.ClientID %>').attr('checked', false)) {
alert("Year Option must be in sequence
order");
return false;
}
else if ($('#<%=chkYear3.ClientID %>').attr('checked', false) && $('#<%= chkYear4.ClientID %>').is(':checked')) {
alert("Year Option must be in sequence
order");
return false;
}
else {
$('#<%=chkYear3.ClientID %>').attr('checked', false)
__doPostBack('<%= chkYear3.ClientID %>', '');
}
}
if (id.indexOf(chk3) > -1) {
if ($('#<%= chkYear1.ClientID %>').is(':checked') && $('#<%= chkYear2.ClientID %>').is(':checked') && $('#<%= chkYear3.ClientID %>').is(':checked') && $('#<%= chkYear4.ClientID %>').is(':checked')) {
__doPostBack('<%= chkYear4.ClientID %>', '');
}
else if ($('#<%=chkYear4.ClientID %>').attr('checked', false) && $('#<%= chkYear1.ClientID %>').is(':checked') && $('#<%= chkYear2.ClientID %>').is(':checked') && $('#<%= chkYear3.ClientID %>').is(':checked')) {
__doPostBack('<%= chkYear4.ClientID %>', '');
}
else {
alert("Year Option must be in sequence
order");
return false;
}
}
}
<asp:CheckBox ID="chkYear1" runat="server" AutoPostBack="true" OnClick="return
ValidatechkSequence(this.id);" OnCheckedChanged="chkYear1_CheckedChanged" /><span>Year
1</span>
<asp:CheckBox ID="chkYear2" runat="server" AutoPostBack="true" OnClick="return ValidatechkSequence(this.id);" OnCheckedChanged="chkYear2_CheckedChanged" /><span>Year
2</span>
<asp:CheckBox ID="chkYear3" runat="server" AutoPostBack="true" OnClick="return
ValidatechkSequence(this.id);" OnCheckedChanged="chkYear3_CheckedChanged" /><span>Year
3</span>
<asp:CheckBox ID="chkYear4" runat="server" AutoPostBack="true" OnClick="return
ValidatechkSequence(this.id);" OnCheckedChanged="chkYear4_CheckedChanged" /><span>Year
0 comments:
Post a Comment