Regex for checking zip code and phone number

I am creating a form with warning elements. Everything works fine, but you need to better define my templates.

                $("#postcode").inputNotes( 
              {
                warning: {
                  pattern: /^[0-9]+$/,
                  type: 'note',
                  text: 'Only numbers, please ...',
                  inversedBehavior: true
                }
              }
            );
            $("#phone").inputNotes( 
              {
                warning: {
                  pattern: /^[0-9]+$/,
                  type: 'note',
                  text: 'Only numbers, no spaces please ...',
                  inversedBehavior: true
                }
              }
            );

Good for postal code warning, I want the template to expect 4 numbers

To alert the phone, I want the template to expect 10 numbers.

Any help was appreciated.

+4
source share
5 answers

Update:

For 6-digit or 10-digit numbers:  pattern: /^[0-9]{10}|[0-9]{6}$/,

For a four-digit postal code: pattern: /^[0-9]{4}$/,

For a 10-digit phone number: (if the phone number cannot begin with 0)  pattern: /^[1-9][0-9]{9}$/,

(if the phone number can start with 0)  pattern: /^[0-9]{10}$/,

+5
source

Here are the rules for Australian zip codes:

ACT: 0200-0299 and 2600-2639.

NSW: 1000-1999, 2000-2599 and 2640-2914.

NT: 0900-0999 and 0800-0899.

QLD: 9000-9999 4000-4999.

SA: 5000-5999.

TAS: 7800-7999 7000-7499.

VIC: 8000-8999 3000-3999.

WA: 6800-6999 6000-6799

: ^ (0 [289] [0-9] {2}) | ([1345689] [0-9] {3}) | (2 [0-8] [0-9 ] {2}) | (290 [0-9]) | (291 [0-4]) | (7 [0-4] [0-9] {2}) | (7 [8-9] [0 -9] {2}) $

Pass: 0200 ||| 7312 ||| 2415

: 0300 ||| 7612 ||| 2915

+8

4- /^\d{4}$/.

10- /^\d{10}$/.

, , .. . , /[^\d]+/g, .

+3

Instead of using a +modifier, try using {num}where numis the number of instances of the previous atom you want.

+2
source

Australia Post significantly simplified the rules in 2019. Now you need to consider only the ranges:
0200-299, 0800-0999 and 1000-9999

So the regex for validating Australian zip codes is simple:

^(0[289][0-9]{2})|([1-9][0-9]{3})$
0
source

All Articles