Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iranian postal code validation

Please, I need to validate Iranian postal code using regex. I write this regex for this case \b([^02\n\D]){4}[^5](\d){5} but its not working on rule number 5 and 7. please help me to fix it.

this is some rules about this regex:

  1. It's all numeric

  2. 10 digit count

  3. don't use 0 in first 5 digit

  4. don't use 2 in postal code

  5. First 4 digit is not the same

  6. The 5th digit cannot be 5

  7. all digits aren't the same

like image 376
MasOOd.KamYab Avatar asked Feb 10 '18 10:02

MasOOd.KamYab


People also ask

Does Iran use postal codes?

Iran does not use zip or postal codes. If it's required for an online forms, use 00000 or other random numbers.

How do you write a postal address in Iran?

Iranian addresses are given in the order of Country, City, Street, Lane, No., Postal code.

What is postal code example?

A United States postal code, for example, might be either five digits or five digits followed a hyphen (dash) and another four digits (12345-1234). If you are validating US postal codes, allow for both conditions. The following rule validates a field that can be five digits, five digits + dash + 4 digits, or blank.


1 Answers

The following regex satisifes your conditions:

\b(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}\b

Click for Demo

Explanation:

  • \b - a word boundary
  • (?!(\d)\1{3}) - negative lookahead to make sure that the 1st 4 digits are not the same.
  • [13-9]{4} - matches 4 occurrences of all the digits except 0 and 2
  • [1346-9] - matches a single digit that is not a 0,2 or 5
  • [013-9]{5} - matches 5 occurrences of all the digits except 2
  • \b - a word boundary
like image 54
Gurmanjot Singh Avatar answered Sep 22 '22 06:09

Gurmanjot Singh