2

I am using a regex in java script to validate a full name field. I need the validation to accept 1 or more instances for any [a-z A-Z] characters and also the field MUST include a - and a space character.

The current regex i am using is : ("^[a-zA-Z\\-\\\s]*$");

This regex is allowing the correct input for what i want, however, it does not force the user to insert a space and a - which i need my validation to include. I am very new to regex and was only introduced to them yesterday so i do not fully understand the correct use of certain characters.

I also do not want the space and - to be at the start or end of the field and not directly next to each other.

examples of valid input:

John smi-th
dereK al-jamone
ro-binstanle y

I know this validation is unusual but I would be very grateful for any help to enable me to get a better understanding of regex.

3 Answers 3

1

Please check whether I understand your requirement correctly:

  • One or more of a-z, A-Z, space (ASCII 32). Exactly one -
  • Space and hyphen - cannot be at start or end of the string, but there must be one space and one hyphen -, so the string must start and end with alphabet character.
  • Hyphen - and space cannot be next to each other. I assume consecutive space and consecutive - are also not allowed.

For the above requirement, this regex should validate the string:

^(?!.*[- ]{2})(?=.* )[a-zA-Z][a-zA-Z ]*-[a-zA-Z ]*[a-zA-Z]$

Short explanation:

  • (?!.*[- ]{2}): Assert that hyphen and space are not next to each other, and also disallow consecutive spaces and hyphens.

  • (?=.* ): Check that at least one space is in the string

  • [a-zA-Z][a-zA-Z ]*-[a-zA-Z ]*[a-zA-Z]: According to my analysis above, the first and last character are alphabet character (and they can't be at the same position in the string) - this will disallow space and hyphen implicitly. Only 1 hyphen is allowed and the rest are any number of alphabet and space.

You can also write this with multiple regex - the code will be slightly clearer:

// No hyphen and space at the beginning and the end. Exactly 1 hyphen.
// The string only contains alphabet, space and hyphen
/^[a-zA-Z][a-zA-Z ]*-[a-zA-Z ]*[a-zA-Z]$/.test(str) &&
// Hyphen and space cannot be directly next to each other
// Consecutive hyphens, or consecutive spaces are also disallowed
!/[- ]{2}/.test(str) &&
// At least one space in the string, can be replaced with indexOf
/ /.test(str)
Sign up to request clarification or add additional context in comments.

2 Comments

@dystroy: I need the validation to accept 1 or more instances for any [a-z A-Z] characters and also the field MUST include a - and a space character. The set doesn't include -, so I assume exactly one.
+1 I'm not sure about the spec (I don't read it as you) but your regex seems to work. I think the answer would be better with a detailed explanation (of the lookahead for example).
0

If you need to ensure that only one space and one hyphen can be present then I would opt for checking two different regex, if either match then it is valid...

with the space BEFORE the hyphen...

^[a-zA-Z]*\s.[a-zA-Z]*\-.[a-zA-Z]*$

with the space AFTER the hyphen

^[a-zA-Z]*\-.[a-zA-Z]*\s.[a-zA-Z]*$

Here is a working example

Comments

0

Is this what you want?

^[A-Za-z\s]+(-(?!-)[A-Za-z\s]+)+$

1 Comment

Space is not allowed in your regex. Have you tested it with OP's example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.