最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How Can I Validate User Name using Regex - Stack Overflow

programmeradmin4浏览0评论

I want to validate the user_name field using REGEX with a Javascript function I want the following requirement for a user_name to be filled in

  • user_name must start with a letter
  • user_name can contains only letters, numbers, underscore and period (.)
  • user_name can not contain white spaces
  • user_name can not be longer than 25 characters

How Can I make a REGEX Expression under the above mention requirements.

I want to validate the user_name field using REGEX with a Javascript function I want the following requirement for a user_name to be filled in

  • user_name must start with a letter
  • user_name can contains only letters, numbers, underscore and period (.)
  • user_name can not contain white spaces
  • user_name can not be longer than 25 characters

How Can I make a REGEX Expression under the above mention requirements.

Share edited Mar 20, 2013 at 13:38 Xophmeister 9,2295 gold badges50 silver badges98 bronze badges asked Mar 20, 2013 at 13:35 Sitara ShaheenSitara Shaheen 593 silver badges5 bronze badges 4
  • 2 Make sure you are also validating the user name on the server side -- client side validation (via javascript) can never be relied on. If the client has javascript disabled, you'd better be checking things on the server! – Chris Baker Commented Mar 20, 2013 at 13:40
  • You should have a bash yourself,first. Here's a great site. – user1945782 Commented Mar 20, 2013 at 13:40
  • In addition to the resource Westie mentioned, here's another useful tool: gskinner./RegExr – Chris Baker Commented Mar 20, 2013 at 13:41
  • 3 What is the problem? Designing the regular expression? Implementing that regular expression in JavaScript? There are plenty of tutorials on JS and regular expressions that you can find via search engines, presumably you have tried them before asking people to investigate your problem personally. What code have you produced so far? – Quentin Commented Mar 20, 2013 at 13:41
Add a ment  | 

3 Answers 3

Reset to default 4

you can try:

   /^[a-z][a-z0-9_\.]{0,24}$/i

Try this regexp:

/^[a-z][\w\.]{0,24}$/i
  • \w matches [a-z0-9_]
  • The /i flag makes the match case-insensitive

Please don't miss reading this ment.

Use [A-z] for the first character. It covers upper and lower case letters.

And you can use [A-z0-9_.]{24} for the other 24 characters.

/[A-z][A-z0-9_\.]{24}/ should do it.

发布评论

评论列表(0)

  1. 暂无评论