skip to Main Content

I’m creating a custom WordPress login form for a BuddyBoss site. The site requires a "nickname" field which has the following requirements:
Only "a-z", "0-9", "-", "_" and "." are allowed.

My form program, FluentForms, has a validation option to match a regex code.

Can I please ask what would the regex code be for the following (not case sensitive):

"a-z", "0-9", "-", "_" and "."

I’ve tried [A-Za-z0-9_-.] but that didn’t seem to work. The form doesn’t submit and shows an error of "Sorry, only "a-z", "0-9", "-", "_" and "." are allowed in Nickname." even when the data entered meets this criteria.

I think fluent forms uses PHP.

Screenshot of Fluent Forms settings

2

Answers


  1. You can do it like this.

    • [a-z0-9._-] – character class for letter, digit, and the three special characters.
    • (?i) – Option flag that says do it case independent
    • + – one or more characters

    So the regex would be:

    (?i)[a-z0-9._-]+
    

    Note: This works fine in Java. But not all regex engines work the same so some changes may be needed. If the regex engine does not support the flags, then simply put A-Z in the character class and omit the (?i) option directive.

    Login or Signup to reply.
  2. I didn’t see the flags in the FluentForms documentation and I would guess it’s either PHP regex and/or JavaScript regex.

    The latter doesn’t support flags inside the regex, but you still could use

    [A-Za-z0-9._-]{1,50}
    

    I added a length validation to the end, {1,50} stands for: minimum 1, maximum 50 characters – it’s always good to have a length validation in public accessible form fields, if you don’t want to bust your database with some scripts that fill up your form fields.

    You can adjust the numbers in there, or Without the length validation, you can replace the {1,50} with a simple + for "one or more characters".

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