I have found this question already but it is not my case. jQuery Validate Uncaught TypeError: Cannot call method 'getAttribute' of undefined . I have a form validation error on form submit Uncaught TypeError: Cannot read property 'getAttribute' of undefined at Function.attributeRules It gives me this error and form gets submitted even the fields are empty. This error comes when user add another and do not select same options for all. For example, if user have 2 dropdowns in the form and he select yes in first and no in other then this error will come and form submit without being validated and if he selects same option in both dropdowns then form will not submit without being validated. What can be the issue?
HTML :
This select element will be visible when page loads :
<select name="engravingOption[]" id="engravingOption_<?php echo $i ?>" class="engravingOptions">
<option value="No" <?php echo((isset($_SESSION['engravingOption']) && $_SESSION['engravingOption'][$i-1]=='No')?'selected="selected"':'');?>>No</option>
<option value="Yes" <?php echo((isset($_SESSION['engravingOption']) && $_SESSION['engravingOption'][$i-1]=='Yes')?'selected="selected"':'');?>>Yes</option>
</select>
If user selects yes then the following field will get visible if no then it stay hidden.
<input type="text" name="engraving_text[]" id="engraving_txt_<?php echo $i ?>" class="element text form-control" maxlength="20" placeholder="Engraving here (20 words)" value="<?php if(isset($_SESSION['engraving_text'])) echo $_SESSION['engraving_text'][$i-1] ?>" onblur="lengthCountOnBlur(this, 'divcount_<?php echo $i; ?>');" onkeyup="limitTextCount(this, 'divcount_<?php echo $i; ?>', 20);" onkeydown="limitTextCount(this, 'divcount_<?php echo $i; ?>', 20);"/>
There is an option to add more fields by cliking Add Another button.
<div id="li_add_another_clipboard">
<button type="button" id="add_another_clipboard">Add Another</button>
</div>
It will add a same dropdown with option of yes and no.
<select name="engravingOption[]" id="engravingOption_<?php echo $add_number ?>" class="engravingOptions">
<option value="No" <?php echo((isset($_SESSION['engravingOption']) && $_SESSION['engravingOption'][$add_number-1]=='No')?'selected="selected"':'');?>>No</option>
<option value="Yes" <?php echo((isset($_SESSION['engravingOption']) && $_SESSION['engravingOption'][$add_number-1]=='Yes')?'selected="selected"':'');?>>Yes</option>
</select>
If user selects yes then the following field will get visible if no then it stay hidden.
<input type="text" name="engraving_text[]" id="engraving_txt_<?php echo $add_number ?>" class="element text form-control" maxlength="20" placeholder="Engraving here (20 words)" value="<?php if(isset($_SESSION['engraving_text'])) echo $_SESSION['engraving_text'][$add_number-1] ?>" onblur="lengthCountOnBlur(this, 'divcount_<?php echo $add_number; ?>');" onkeyup="limitTextCount(this, 'divcount_<?php echo $add_number; ?>', 20);" onkeydown="limitTextCount(this, 'divcount_<?php echo $add_number; ?>', 20);" required/>
This is How I am validating form :
$('#form').validate({
rules: {
"clipboardQuantity[]":"required",
"clipboard_color[]":"required",
"clipboard_label[]":"required",
"engraving_text[]":"required",
},
submitHandler: function(form) {
var $form = $(form);
$.ajax({
url: $form.action,
type: $form.method,
data: $form.serialize(),
success: function(response) {
if (response == false)
{alert('could not submit!')}
}
});
}
});
UPDATE:
this function works fine but still if user select YES in one select element and NO in other select element then blank form submitted. Why validations fail to stop blank form from being submitted?
var clipboardsCount=Number('<?php if(isset($_SESSION["clipboardsCount"])) echo $_SESSION["clipboardsCount"]?>');if(clipboardsCount == 0){clipboardsCount=1;}
$(document).on('change', '.engravingOptions', function (e) {
for(var i=1; i<= clipboardsCount; i++) {
var value = $("#engravingOption_"+i).val();
if (value == 'No') {
$('#li_engraving_txt_'+i).hide();
$('#engraving_txt_'+i).val('');
}
else if (value == 'Yes') {
$('#li_engraving_txt_'+i).show();
}
}
});
UPDATE 2 :
This is the code for which it gives error, I don't understand why? var rules = {}, $element = $( element ), type = element.getAttribute( "type" ), method, value;
attributeRules: function( element ) {
var rules = {},
$element = $( element ),
type = element.getAttribute( "type" ),
method, value;
for ( method in $.validator.methods ) {
// support for <input required> in both html5 and older browsers
if ( method === "required" ) {
value = element.getAttribute( method );
// Some browsers return an empty string for the required attribute
// and non-HTML5 browsers might have required="" markup
if ( value === "" ) {
value = true;
}
// force non-HTML5 browsers to return bool
value = !!value;
} else {
value = $element.attr( method );
}
// convert the value to a number for number inputs, and for text for backwards compability
// allows type="date" and others to be compared as strings
if ( /min|max/.test( method ) && ( type === null || /number|range|text/.test( type ) ) ) {
value = Number( value );
}
if ( value || value === 0 ) {
rules[ method ] = value;
} else if ( type === method && type !== "range" ) {
// exception: the jquery validate 'range' method
// does not test for the html5 'range' type
rules[ method ] = true;
}
}
// maxlength may be returned as -1, 2147483647 ( IE ) and 524288 ( safari ) for text inputs
if ( rules.maxlength && /-1|2147483647|524288/.test( rules.maxlength ) ) {
delete rules.maxlength;
}
return rules;
},