validate.js icon indicating copy to clipboard operation
validate.js copied to clipboard

Can't validate fields with name like "fieldname[]"

Open hanialaraj opened this issue 10 years ago • 2 comments

If you have select element like this: <select name="degree[]"> <option value="">Select</option> <option value="High School" >High School</option> <option value="Diploma" >Diploma</option> <option value="BA" >BA</option> </select>

new FormValidator('applyform', [{ name: 'degree[]', display: 'Degree', rules: 'required'}], function(errors, e){ /* if(errors.length > 0) is always true even if "degree[]" select is selected*/ });

Fix suggestion, inside attributeValue plugin function, before the last "return", I added

if ((attributeName == 'value') && (element.length > 0) && (element[0].type === 'select-one')) { _v = new Array(); for (i = 0, elementLength = element.length; i < elementLength; i++) { if(element[i][attributeName] && element[i][attributeName].length > 0){ _v.push(element[i][attributeName]); }else{ return ''; // stop looping, found empty value! } } return _v; // return array of collected values }

hanialaraj avatar Apr 09 '15 12:04 hanialaraj

A pull request would be much appreciated in this area :)

rickharrison avatar Apr 09 '15 22:04 rickharrison

Hi, @rickharrison I would like to work on this issue. Can you please assign me?

shehriyarnadeem avatar Aug 08 '22 13:08 shehriyarnadeem