Skip to content

Commit 99d7645

Browse files
author
Siddhant
committed
added require_from_group validation for inputs with array names
1 parent 409f631 commit 99d7645

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

demo/demo-with-params.html

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<!DOCTYPE html>
22
<html>
3-
43
<head>
54
<title>JQuery Validate Wrapper</title>
65
<meta charset = "utf-8">
@@ -47,7 +46,7 @@
4746
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
4847
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
4948
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>
50-
<script src="../jquery.validateWrapper.min.js"> </script>
49+
<script src="../jquery.validateWrapper.js"> </script>
5150
<script>
5251
$(function () {
5352

@@ -68,7 +67,7 @@
6867
g_name_group: "first_name last_name",
6968
},
7069
require_from_group: {
71-
rfg_dob_group: 2,
70+
rfg_dob_group: 2
7271
},
7372
highlight: function (element, errorClass, validClass) {
7473
if(jQuery(element).next().hasClass('input-group-append'))
@@ -111,10 +110,15 @@
111110
else if (element.is('input[type=checkbox]') || element.is('input[type=radio]'))
112111
element.closest('.form-check').parent().append(error);
113112
else if (element.hasClass('required-from-group')) {
114-
let lastElement = $('.rfg_dob_group:last');
115-
if ($(element).attr('name') == $(lastElement).attr('name'))
116-
error.insertAfter(lastElement.parent().parent());
117-
} else if (element.hasClass('group-in-one')) {
113+
if(element.hasClass('rfg_dob_group')) {
114+
let lastElement = $('.rfg_dob_group:last');
115+
if ($(element).attr('name') == $(lastElement).attr('name'))
116+
error.insertAfter(lastElement.parent().parent());
117+
}else{
118+
error.insertAfter(element);
119+
}
120+
}
121+
else if (element.hasClass('group-in-one')) {
118122
let lastEl = $('.g_name_group:last');
119123
error.insertAfter(lastEl.parent().parent());
120124
} else
@@ -151,12 +155,10 @@
151155
});
152156
}
153157
}
154-
155158
//this is my callback function
156159
const submitData = (form, data) => console.log('Submit Data function is called..');
157160
</script>
158161
</head>
159-
160162
<body>
161163
<div class="container">
162164
<div class="clearfix" style="height:30px;"></div>
@@ -273,5 +275,4 @@
273275
</div>
274276
</div>
275277
</body>
276-
277278
</html>

jquery.validateWrapper.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
2-
* JQuery validateWrapper Plugin v1.19.1
3-
* Works well for jQuery validate Plugin v1.19.1
2+
* JQuery validateWrapper Plugin v1.19.2
3+
* Works well for jQuery validate Plugin v1.19.2
44
* Copyright (c) 2019 Siddhant Naik
55
*
66
* Plugin Name: validateWrapper
@@ -133,10 +133,18 @@
133133
if (i == j) {
134134
if (plugin._messages[j] != '') {
135135
$('.' + j).each(function () {
136-
if ($("[name=" + this.name + "]", plugin.$_element).length) {
137-
$("[name=" + this.name + "]", plugin.$_element).rules("add", {
138-
messages: { 'require_from_group': plugin._messages[j] }
136+
if($('input[name^="'+this.name+'"]', plugin.$_element).length) {
137+
$('input[name^="'+this.name+'"]', plugin.$_element).each(function() {
138+
$("#"+this.id, plugin.$_element).rules("add", {
139+
messages: { 'require_from_group': plugin._messages[j] }
140+
});
139141
});
142+
}else {
143+
if ($("[name=" + this.name + "]", plugin.$_element).length) {
144+
$("[name=" + this.name + "]", plugin.$_element).rules("add", {
145+
messages: { 'require_from_group': plugin._messages[j] }
146+
});
147+
}
140148
}
141149
});
142150
}

jquery.validateWrapper.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)