admin管理员组文章数量:1290949
I would like to create a regex pattern in javascript for a html text field
that takes an address. So I only want the user to enter [a-z,A-Z,0-9]
and only these symbols [,#-.]
I also want to prevent the user from sending an empty string and the maximum number of characters to be less than 100.
This is the html <input>
:
<input type="text" id="addy" maxlength="100"/>
I'm new to javascript, so I do not know how to create a function that will enable the regex patterns to be adhered to.
Thanks
EDIT
Following the remendation by kolink
I tried this:
<input type="text" style="width:285px" placeholder="Enter A Precise Address" name="address0" id="address"
pattern="[ a-zA-Z0-9,#.-]+" maxlength="100" title="Standard address notation only"/>
This now only works if the other <select>
have not been selected. Otherwise it does not pick up the pattern
attribute. However the exact same code works as expected on jsfiddle: / but not in the browser. This is the code in pastebin:
Thanks
I would like to create a regex pattern in javascript for a html text field
that takes an address. So I only want the user to enter [a-z,A-Z,0-9]
and only these symbols [,#-.]
I also want to prevent the user from sending an empty string and the maximum number of characters to be less than 100.
This is the html <input>
:
<input type="text" id="addy" maxlength="100"/>
I'm new to javascript, so I do not know how to create a function that will enable the regex patterns to be adhered to.
Thanks
EDIT
Following the remendation by kolink
I tried this:
<input type="text" style="width:285px" placeholder="Enter A Precise Address" name="address0" id="address"
pattern="[ a-zA-Z0-9,#.-]+" maxlength="100" title="Standard address notation only"/>
This now only works if the other <select>
have not been selected. Otherwise it does not pick up the pattern
attribute. However the exact same code works as expected on jsfiddle: http://jsfiddle/peD3t/7/ but not in the browser. This is the code in pastebin: http://pastebin./qQGJ4EK5
Thanks
Share Improve this question edited Dec 28, 2012 at 23:35 joe asked Dec 28, 2012 at 21:45 joejoe 351 gold badge1 silver badge7 bronze badges 2- Show your part of code for this task and people will help with it – edem Commented Dec 28, 2012 at 21:49
- Try something, post it here, and explain what is not working. This is not a do it for me forum. You'll learn when you've tried and failed and then succeeded – Ruan Mendes Commented Dec 28, 2012 at 21:49
2 Answers
Reset to default 8HTML5 gives you the pattern
attribute, which allows you to require a certain pattern. In this case, you'd want pattern="[a-zA-Z0-9,#.-]+"
It doesn't matter that older browser don't support this, because you should always validate on the server side.
$(document).ready(function () {
$('input').keyup(function() {
var $th = $(this);
$th.val( $th.val().replace(/[^a-zA-Z0-9,#.-]/g, function(str) { return ''; } ) );
});
});
This will work.
本文标签: javascriptRegex Patterns for HTML5 Text FieldStack Overflow
版权声明:本文标题:javascript - Regex Patterns for HTML5 Text Field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741510918a2382601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论