Home > other >  HTML address validation pattern
HTML address validation pattern

Time:11-17

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,}

Thanks in advance.

CodePudding user response:

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';
  • Related