开发者

Validating HTML text box with javascript and regex

T开发者_如何学JAVAhe below code works to only allow alphanumerics and spaces. However, I would like to also allow an accented character (Ã). How should the regex be modified?

Thanks

<html>
<head>
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/JavaScript">
 $(function() {
  $("#sub").bind("click",
   function() {
    $('#addText').val($('#addText').val().replace(new RegExp("[^a-zA-Z0-9 ]","g"), ''));
  });
 });
</script>
</head><body>
 <div>Enter Text:</div>
 <input id="addText" type=text/>
 <input id="sub"  type="button" value="Submit" />
</body></html>


If you only care about Latin-1 (Western European) letters, this should work:

[A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\xff]

For other scripts (eg: Greek, Cyrillic, Thai letters, CJK characters, etc.) things get much more complicated, and it becomes safer to just forbid things like control characters, rather than trying to keep track of which characters are "letters".


If you just want to add this one character .. just add it in the regex

[^a-zA-Z0-9Ã ]

Keep in mind though that there might be complications as mentioned below so do follow the suggestion by Laurence Gonsalves

  • http://unicode.org/reports/tr18/
  • http://www.regular-expressions.info/unicode.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜