skip to Main Content

I want to create a HTML pattern for a field as (Display name). There isn’t any more complex file, just a HTML file with one field.

The requirements are:

  1. The starting character of the field can just be a uppercase letter
    and following letters should be lowercase but can be any kind of
    characters (number, special character)
  2. Can’t start with space and can’t use consecutive spaces
  3. Words after space can start with (letter, number, special
    character) but if its a letter should be uppercase

I could write the regex expression that match the most of the requirements but special characters aren’t being included.

^[A-Z][a-z]*( [A-Z][a-z]*)*$

but I want to use special characters and numbers anywhere (except the first letter) and problem is happening here, this is the pattern:

^[A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]*( [A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]*)*$

and I’m getting this error on console:

this causing this error: Unable to check <input pattern=‘^[A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^|~]*( [A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^|~]*)*$’> because ‘/^[A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^|~]*( [A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^|~]*)*$/v’ is not a valid regexp: invalid character in class in regular expression

I tried many other things, but using special characters is a troublemaker for me. Would you please tell me what’s the correct pattern for it?

Thanks.

This is the whole code:

const registrationForm = document.querySelector('#registration-form');

registrationForm.onsubmit = e =>
  {
  e.preventDefault();  // 4 testing mode
  console.clear();
  console.log( ':) name  accepted ', registrationForm.display_name.value);
  }
<form id="registration-form">
  <!-- Display Name Field -->
  <label for="display-name">Display Name:</label>
  <input type="text" id="display-name" name="display_name" pattern="^[A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]*(( [A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]*)| (?! )[a-zA-Z0-9@()[]{}._-!#$%&'*+/=?^`|~]*)*$" title="Display name must start with a letter, have no consecutive spaces, and each word must start with an uppercase letter. Length must be between 2 and 50 characters."
    minlength="2" maxlength="50" required>
  <br><br>

  <button type="submit">Submit</button>
</form>

3

Answers


  1. The issue is with the way special characters are escaped and defined in your regular expression. Specifically, the ] character within the character set (enclosed in square brackets []) must be escaped properly because it is a metacharacter that denotes the end of a character set.

    Here’s the corrected regex:

    ^[A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]*( [A-Z][a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]*)*$
    
    Login or Signup to reply.
  2. Try:

    ^[A-Z](?:[a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]|s[A-Z0-9@()[]{}._-!#$%&'*+/=?^`|~])*$
    

    See: regex101


    Explanation

    • ^...$: Anchor to ensure whole field is matched
    • [A-Z]: First upper case letter
    • (?: ... )*: Then repeat either
      • [a-z0-9@()[]{}._-!#$%&'*+/=?^`|~]: Lower case or special character
      • |: or
      • s[A-Z0-9@()[]{}._-!#$%&'*+/=?^`|~]: Space followed by uppercase or special character
    Login or Signup to reply.
  3. It is not clear what you mean by "special character".
    I have chosen to define it as anything that is not: an upper or lower case letter, a digit, or whitespace.

    ^p{Lu}[^p{Lu}p{Z}]*(?:p{Z}[^p{Ll}p{Z}][^p{Lu}p{Z}]*)*$
    

    Just edit the bits between [] to change the definition to exclude more.

    For information on unicode character categories, see:
    https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-4/#G124142

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search