You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用正则表达式验证注册页面的用户名字?满足指定格式要求

Validating Names with Regex: Your Requirements Covered

Got it, let's work through this regex problem step by step. First, let's make sure I've got your requirements straight:

  • The name can only contain letters (no numbers, spaces, symbols, or any non-alphabet characters)
  • The first letter doesn't have to be capitalized (so all-lowercase names are totally fine)
  • No uppercase letters allowed anywhere else in the name (so middle/end characters must be lowercase)

The Regex Pattern

Here's the regex that meets all these rules:

^[a-zA-Z][a-z]*$

Breaking Down the Pattern

Let's unpack each part so you understand exactly what's happening:

  • ^: Anchors the match to the start of the string (ensures we're checking the entire input, not just a substring)
  • [a-zA-Z]: Matches a single letter—either uppercase or lowercase (covers the first character, which can be capitalized or not)
  • [a-z]*: Matches zero or more lowercase letters (the * means it works for single-letter names too, and ensures no uppercase sneaks into the middle/end)
  • $: Anchors the match to the end of the string (prevents any extra non-letter characters from slipping through)

Test Cases: What Works & What Doesn't

Valid Names (Will Match)

  • lisa (all lowercase, fully allowed)
  • Lisa (first letter capitalized, rest lowercase)
  • z (single lowercase letter)
  • K (single uppercase letter)

Invalid Names (Won't Match)

  • lIsA (has uppercase letters in the middle)
  • LisaMarie (the M is uppercase, which violates the "no middle uppercase" rule)
  • lisa123 (contains numbers)
  • lisa ann (contains a space, a non-letter character)
  • LISA (all uppercase—middle characters are uppercase, which is not allowed)

Optional: Minimum Length Requirement

If you want to enforce that names are at least 2 characters long (no single-letter names), just swap the * with a + (which means "one or more" instead of "zero or more"):

^[a-zA-Z][a-z]+$

Example Code Snippets

Here's how you might use this regex in common languages:

Python

import re

def is_valid_name(name):
    name_pattern = r'^[a-zA-Z][a-z]*$'
    return re.fullmatch(name_pattern, name) is not None

# Test it out
print(is_valid_name("michael"))  # True
print(is_valid_name("Michael"))  # True
print(is_valid_name("mIchAel"))  # False

JavaScript

function isValidName(name) {
  const namePattern = /^[a-zA-Z][a-z]*$/;
  return namePattern.test(name);
}

console.log(isValidName("emily")); // true
console.log(isValidName("Emily")); // true
console.log(isValidName("emIly")); // false

If you had a different interpretation of your requirements (like if you meant no uppercase letters at all, even the first one), just let me know and we can adjust the regex!

内容的提问来源于stack exchange,提问作者Peleg Egozi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:32:12