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
Entity Framework Creates a Plural Table Name, But the View Expects a Singular Table Name
Removing Hidden Characters from Within Strings
Process.Start to Open an Url, Getting an Exception
"Nested Foreach" VS "Lambda/Linq Query" Performance(Linq-To-Objects)
How to Get Character for a Given Ascii Value
How to Use C# 8 with Visual Studio 2017
Client Id for Property (ASP.NET MVC)
Passing Parameters to Xslt Stylesheet via .Net
How to Disable Visual Styles for Just One Control, and Not Its Children
Visualizing an Ast Created with Antlr (In a .Net Environment)
How to Make Correct Date Format When Writing Data to Excel
Linq: from a List of Type T, Retrieve Only Objects of a Certain Subclass S
Newtonsoft JSON Deserialize Dictionary as Key/Value List from Datacontractjsonserializer
How to Change the Culture of a Winforms Application at Runtime
Object' Does Not Contain a Definition for 'X'
Using Graphics.Drawimage() to Draw Image with Transparency/Alpha Channel