Canadian Postal Code Validation

Efficient regex for Canadian postal code function

User kind, postal code strict, most efficient format:

/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i

Allows:

  • h2t-1b8
  • h2z 1b8
  • H2Z1B8

Disallows:

  • Z2T 1B8 (leading Z)
  • H2T 1O3 (contains O)

Leading Z,W or to contain D, F, I, O, Q or U

Canadian postal code validation

Canadian postal codes can't contain the letters D, F, I, O, Q, or U, and cannot start with W or Z:

[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]

If you want an optional space in the middle:

[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]

javascript regex for canadian postal code

function checkPostal(postal) {
var regex = new RegExp(/^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]( )?\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i);
if (regex.test(postal.value))
return true;
else return false;
}

Validation of Canadian postal code fails when whitespace inside

You have used kb.next() which will read data with space separation,
So when you will enter postal code as A1A 1A1 it will take it 2 times first one is A1A and second one is 1A1 and therefor you will get invalid output 2 times as it will take 2 time data with one space, so you need to use nextLine() it will help you to resolve your issue,

Check below resolved answer

public class validatePostalCodeTest 
{
public static void main (String[] args) throws java.lang.Exception
{

Scanner kb = new Scanner(System.in);
System.out.println("Please enter postalcode:");
String posCode = kb.nextLine();

if (posCode.length() > 7)
System.out.println("\nInvalid");
if (posCode.length() < 6)
System.out.println("\nInvalid");
if (posCode.length()== 7){
boolean valid = true;

char a = posCode.charAt(0);
char b = posCode.charAt(2);
char c = posCode.charAt(4);
char d = posCode.charAt(1);
char e = posCode.charAt(5);
char f = posCode.charAt(6);
char g = posCode.charAt(3);
if(! Character.isLetter(a))
valid = false;
else if (! Character.isLetter(b))
valid = false;
else if (! Character.isDigit(c))
valid = false;
else if (! Character.isDigit(d))
valid = false;
else if (! Character.isLetter(e))
valid = false;
else if (! Character.isDigit(f))
valid = false;
else if (! Character.isWhitespace(g))
valid = false;

if (valid) System.out.println("\nValid");
else System.out.println("\nInvalid");
}
if (posCode.length()== 6){
boolean valid = true;

char a = posCode.charAt(0);
char b = posCode.charAt(2);
char c = posCode.charAt(4);
char d = posCode.charAt(1);
char e = posCode.charAt(3);
char f = posCode.charAt(5);
if(! Character.isLetter(a))
valid = false;
else if (! Character.isLetter(b))
valid = false;
else if (! Character.isLetter(c))
valid = false;
else if (! Character.isDigit(d))
valid = false;
else if (! Character.isDigit(e))
valid = false;
else if (! Character.isDigit(f))
valid = false;

if (valid) System.out.println("\nValid");
else System.out.println("\nInvalid");
}
System.out.println("Program ending due to end-of-file");
}
}

Validate text input to match Canadian postal code (e.g. A1S2S3)

You can do it like this:

<input type="text" id="postCode" placeholder="Enter postcode">
<span id="result"></span>

var pattern = /[ABCEFGHJKLMNPRSTVXY][0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9][ABCEFGHJKLMNPRSTVWXYZ][0-9]/,
$result = $("#result");

$('#postCode').keyup(function(){
var val = this.value
if(!val.match(pattern)){
$result.text("invalid");
} else {
$result.text("valid");
}
});

Here's a demo

Restrictive RegEx for validate Canada postal code

If all you need is a regex for canada postal code, try this:

^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$

It should do exactly what you're looking for



Related Topics



Leave a reply



Submit