|
309 | 309 |
|
310 | 310 | // Helper function which checks for blank inputs in a form that match the specified CSS selector
|
311 | 311 | blankInputs: function(form, specifiedSelector, nonBlank) {
|
312 |
| - var inputs = $(), input, valueToCheck, |
313 |
| - selector = specifiedSelector || 'input,textarea', |
314 |
| - allInputs = form.find(selector); |
315 |
| - |
316 |
| - allInputs.each(function() { |
| 312 | + var foundInputs = $(), |
| 313 | + input, |
| 314 | + valueToCheck, |
| 315 | + radiosForNameWithNoneSelected, |
| 316 | + radioName, |
| 317 | + selector = specifiedSelector || 'input,textarea', |
| 318 | + requiredInputs = form.find(selector), |
| 319 | + checkedRadioButtonNames = {}; |
| 320 | + |
| 321 | + requiredInputs.each(function() { |
317 | 322 | input = $(this);
|
318 |
| - valueToCheck = input.is('input[type=checkbox],input[type=radio]') ? input.is(':checked') : !!input.val(); |
319 |
| - if (valueToCheck === nonBlank) { |
| 323 | + if (input.is('input[type=radio]')) { |
320 | 324 |
|
321 |
| - // Don't count unchecked required radio if other radio with same name is checked |
322 |
| - if (input.is('input[type=radio]') && allInputs.filter('input[type=radio]:checked[name="' + input.attr('name') + '"]').length) { |
323 |
| - return true; // Skip to next input |
324 |
| - } |
| 325 | + // Don't count unchecked required radio as blank if other radio with same name is checked, |
| 326 | + // regardless of whether same-name radio input has required attribute or not. The spec |
| 327 | + // states https://www.w3.org/TR/html5/forms.html#the-required-attribute |
| 328 | + radioName = input.attr('name'); |
| 329 | + |
| 330 | + // Skip if we've already seen the radio with this name. |
| 331 | + if (!checkedRadioButtonNames[radioName]) { |
| 332 | + |
| 333 | + // If none checked |
| 334 | + if (form.find('input[type=radio]:checked[name="' + radioName + '"]').length === 0) { |
| 335 | + radiosForNameWithNoneSelected = form.find( |
| 336 | + 'input[type=radio][name="' + radioName + '"]'); |
| 337 | + foundInputs = foundInputs.add(radiosForNameWithNoneSelected); |
| 338 | + } |
325 | 339 |
|
326 |
| - inputs = inputs.add(input); |
| 340 | + // We only need to check each name once. |
| 341 | + checkedRadioButtonNames[radioName] = radioName; |
| 342 | + } |
| 343 | + } else { |
| 344 | + valueToCheck = input.is('input[type=checkbox],input[type=radio]') ? input.is(':checked') : !!input.val(); |
| 345 | + if (valueToCheck === nonBlank) { |
| 346 | + foundInputs = foundInputs.add(input); |
| 347 | + } |
327 | 348 | }
|
328 | 349 | });
|
329 |
| - return inputs.length ? inputs : false; |
| 350 | + return foundInputs.length ? foundInputs : false; |
330 | 351 | },
|
331 | 352 |
|
332 | 353 | // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
|
|
0 commit comments