0

Can any one help me with regex patter to allow address validation based on below limitations:

  • Allow only alphanumeric characters, spaces, apostrophes ('), dashes (-), commas, (,), periods (.), number signs (#), and slashes (/),
  • Must contain at least one numeral, one alphabetic character, and one space.

I tried below patterns:

/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
(?=.*\d)(?=.* ).{8,}

1 Answer 1

2

You can use

^(?=\S*\s)(?=[^a-zA-Z]*[a-zA-Z])(?=\D*\d)[a-zA-Z\d\s',.#/-]*$

Or, a Unicode variation:

^(?=\S*\s)(?=\P{L}*\p{L})(?=\D*\d)[\p{L}\d\s',.#/-]*$

See the regex demo.

Details:

  • ^ - start of string
  • (?=\S*\s) - at least one whitespace required
  • (?=[^a-zA-Z]*[a-zA-Z]) - at least one letter
  • (?=\D*\d) - at least one digit
  • [a-zA-Z\d\s',.#/-]* - zero or more letters, digits, whitespaces, ', ,, ., #, / or - (replace * with + to require at least one char in the string)
  • $ - end of string.

Declaration in PHP:

$regex = '~^(?=\S*\s)(?=[^a-zA-Z]*[a-zA-Z])(?=\D*\d)[a-zA-Z\d\s\',.#/-]*$~';
$regex = '~^(?=\S*\s)(?=\P{L}*\p{L})(?=\D*\d)[\p{L}\d\s\',.#/-]*$~u';
Sign up to request clarification or add additional context in comments.

4 Comments

I have used your pattern like below, but its not working. <input type="text" name="address" placeholder="e.g. 123 River Rd. Apt 45" pattern="(?=\S*\s)(?=\P{L}*\p{L})(?=\D*\d)[\p{L}\d\s',.#/-]*$" required>
@AkshayPaghdar You must use pattern="(?=\S*\s)(?=[^a-zA-Z]*[a-zA-Z])(?=\D*\d)[a-zA-Z\d\s',.#/-]*"
Can you help me with this too please:- Name Validation: Can only contain letters, spaces, apostrophes ('), dashes (-), commas, (,), periods (.). Must be at least 2 characters long and can only start with a letter. ?
@AkshayPaghdar pattern="[a-zA-Z][a-zA-Z\s\x27,.-]+"

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.