1

I am trying to add a regular expression in my AngularJS controller that validates the four following conditions in a text input box: A) name (space) name, B) name,(comma) name, C) 123456789 (max of 9 digits) D) 123-45-6789

I created the following regex: 1) validation: /^[a-zA-Z0-9 ,\-]+[(?<=\d\s]([a-zA-Z0-9]+\s)*[a-zA-Z0-9]+$ /,

This is close to what I want with the exception of formatting the last four digits and limiting digits to a max of nine characters.

I also tried combining the following regex per my research to no avail: 2) validation: /^([0-9]{8,9})$/ + /^([A-z'\/-])+[\s]+([A-z'\/-])+$/ + /^([A-z'\/-])+[\s]*[\,]{1}[\s]*([A-z-\/'])+$/ + /^\d{3}-\d{2}-\d{4}$/,

3) validation: /^([0-9]{8,9})$/ | /^([A-z'\/-])+[\s]+([A-z'\/-])+$/ | /^([A-z'\/-])+[\s]*[\,]{1}[\s]*([A-z-\/'])+$/ | /^\d{3}-\d{2}-\d{4}$/,

4) validation: new RegExp('(' + /^([0-9]{8,9})$/ + ") | (" + /^([A-z'\/-])+[\s]+([A-z'\/-])+$/ + ") | (" + /^([A-z'\/-])+[\s]*[\,]{1}[\s]*([A-z-\/'])+$/ + ") | (" + /^\d{3}-\d{2}-\d{4}$/ + ')'),

I have three questions:

  • Is it possible to create one regex that validates these four conditions?

  • Am I not combining these correct?

  • Or should I abandon this regex mission and just create a separate function to perform my validation?

4
  • Maybe I'm understanding something wrong, but why wouldn't just combining them with | work? Commented Feb 14, 2017 at 18:32
  • I would think it would work but for some reason the system seems to ignore the regex after the | symbol Commented Feb 14, 2017 at 18:46
  • Maybe because your first regex has unclosed parenthesis? Commented Feb 14, 2017 at 18:47
  • Thanks, I'm not sure I understand, '(' + /^([0-9]{8,9})$/ + ") isn't that a closed parenthesis? Commented Feb 14, 2017 at 19:05

2 Answers 2

1

This should work for you: ^([a-zA-Z]+(?:-\/)?[,\/]?\s+[a-zA-Z]+(?:-\/)?|\d{1,9}|\d{3}-\d{2}-\d{4})$

const validate = input => /^^([a-zA-Z]+(?:-\/)?[,\/]?\s+[a-zA-Z]+(?:-\/)?|\d{1,9}|\d{3}-\d{2}-\d{4})$/.test(input)

const testData = ["123", "name space", "John Doe", "John/ Doe", "Doe, John", "John", "name, ", "John   Doe", "John-/   Doe", "John-/, Doe-/", "sdf", "123-45-6789", "1234567890" ,"name , surname"]
for (const input of  testData){
  console.log(`${input} ${validate(input)}`)
}

Sign up to request clarification or add additional context in comments.

13 Comments

this is really cool but it allows one name to be processed. so John should be invalid criteria but John Smith or Smith, John should be good.
@Eddie ill fix it in second
@Eddie now it's fixed as you wanted but i have one question it has to bo one space or one or more ?
That's a great question. Just one space is allowed between names or Smith, John.
@Eddie check now it's propably okay
|
1

You could go for

^(?:(?:[a-z]+,?[ ]+[a-z]+)|(?:\d{3}-\d{2}-\d{4})|(?:\d+))$

See a demo on regex101.com.


Broken down, it reads:

^                          # match beginning of the line
(?:                        # non-capturing parenthesis
    (?:[a-z]+,?[ ]+[a-z]+) # a-z then comma (optional) space then a-z again
    |                      # or
    (?:\d{3}-\d{2}-\d{4})  # the number format with dashes
    |                      # or
    (?:\d+)                # only numbers
)
$                          # match the end of the line

Comments

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.