admin管理员组文章数量:1024593
Alright, so check this out
/
<form class="validation">
<div>
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</div>
</form>
I'm working on a form validation script and want to use the HTML5 attributes for this. I'm not sure if this is the most handy way (tips are wele) but in this case I'm trying to get the minimum amount and maximum amount of allowed characters.
If you look at the console, everything seems to work fine apart from the "Uncaught TypeError: Cannot read property 'length' of undefined" error. It is strange since it isn't even undefined.
So this fixes it /
<form class="validation">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</form>
I removed the surrounding element and now it works but without any errors.
How to fix this? And is there a better way to do this? (might be a bit off-topic)
Greetings
Alright, so check this out
http://jsfiddle/J9Tza/
<form class="validation">
<div>
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</div>
</form>
I'm working on a form validation script and want to use the HTML5 attributes for this. I'm not sure if this is the most handy way (tips are wele) but in this case I'm trying to get the minimum amount and maximum amount of allowed characters.
If you look at the console, everything seems to work fine apart from the "Uncaught TypeError: Cannot read property 'length' of undefined" error. It is strange since it isn't even undefined.
So this fixes it http://jsfiddle/J9Tza/1/
<form class="validation">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</form>
I removed the surrounding element and now it works but without any errors.
How to fix this? And is there a better way to do this? (might be a bit off-topic)
Greetings
Share Improve this question asked Oct 19, 2013 at 0:55 Maarten de GraafMaarten de Graaf 5262 gold badges5 silver badges21 bronze badges 1- you should include the code here – Ibu Commented Oct 19, 2013 at 0:57
1 Answer
Reset to default 2it is because of event propagation, the keyup
event is propagated to the div
(which is selected because of the selector :not([type])
) element where there is no pattern
attribute. So pattern = that.attr('pattern')
will be undefined
You need to tweak the :not([type])
selector to select only input elements without type
attribute. But you can bine the [type="text"], :not([type])
selectors to input:text
.
Also since you are interested in only elements with pattern
attribute you can add it also to the selector
$('form.validation').find('input:text[pattern], input[type="email"][pattern], input[type="password"][pattern]').each(function () {
$(this).on('keyup', function () {
var that = $(this),
pattern = that.attr('pattern');
console.log(this, pattern)
pattern_array = pattern.substr(2, pattern.length - 3).split(','),
min_characters = pattern_array[0],
max_characters = pattern_array[1];
console.log(min_characters);
});
});
Demo: Fiddle
Alright, so check this out
/
<form class="validation">
<div>
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</div>
</form>
I'm working on a form validation script and want to use the HTML5 attributes for this. I'm not sure if this is the most handy way (tips are wele) but in this case I'm trying to get the minimum amount and maximum amount of allowed characters.
If you look at the console, everything seems to work fine apart from the "Uncaught TypeError: Cannot read property 'length' of undefined" error. It is strange since it isn't even undefined.
So this fixes it /
<form class="validation">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</form>
I removed the surrounding element and now it works but without any errors.
How to fix this? And is there a better way to do this? (might be a bit off-topic)
Greetings
Alright, so check this out
http://jsfiddle/J9Tza/
<form class="validation">
<div>
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</div>
</form>
I'm working on a form validation script and want to use the HTML5 attributes for this. I'm not sure if this is the most handy way (tips are wele) but in this case I'm trying to get the minimum amount and maximum amount of allowed characters.
If you look at the console, everything seems to work fine apart from the "Uncaught TypeError: Cannot read property 'length' of undefined" error. It is strange since it isn't even undefined.
So this fixes it http://jsfiddle/J9Tza/1/
<form class="validation">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" required="required">
</form>
I removed the surrounding element and now it works but without any errors.
How to fix this? And is there a better way to do this? (might be a bit off-topic)
Greetings
Share Improve this question asked Oct 19, 2013 at 0:55 Maarten de GraafMaarten de Graaf 5262 gold badges5 silver badges21 bronze badges 1- you should include the code here – Ibu Commented Oct 19, 2013 at 0:57
1 Answer
Reset to default 2it is because of event propagation, the keyup
event is propagated to the div
(which is selected because of the selector :not([type])
) element where there is no pattern
attribute. So pattern = that.attr('pattern')
will be undefined
You need to tweak the :not([type])
selector to select only input elements without type
attribute. But you can bine the [type="text"], :not([type])
selectors to input:text
.
Also since you are interested in only elements with pattern
attribute you can add it also to the selector
$('form.validation').find('input:text[pattern], input[type="email"][pattern], input[type="password"][pattern]').each(function () {
$(this).on('keyup', function () {
var that = $(this),
pattern = that.attr('pattern');
console.log(this, pattern)
pattern_array = pattern.substr(2, pattern.length - 3).split(','),
min_characters = pattern_array[0],
max_characters = pattern_array[1];
console.log(min_characters);
});
});
Demo: Fiddle
本文标签:
版权声明:本文标题:javascript - Uncaught TypeError: Cannot read property 'length' of undefined when there is a parent - Stack Overf 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745559959a2156104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论