I'm doing validation on Australian DVA numbers, the rules are:
Here is my current attempt and it's working fine:
if (strlen($value) == 9 && preg_match("/^[NVQWST][A-Z\s]{1,3}[0-9]{1,6}[A-Z]$/", $value)) {
return true;
}
if (strlen($value) == 8 && preg_match("/^[NVQWST][A-Z\s]{1,3}[0-9]{1,6}$/", $value)) {
return true;
}
return false;
My question: Is there any way that I can combine these conditions in 1 regex check?
You can use
^(?=.{8,9}$)[NVQWST][A-Z\s]{1,3}[0-9]{1,6}(?:(?<=^.{8})[A-Z])?$
See the regex demo.
Details
^
- start of a string(?=.{8,9}$)
- the string should contain 8 or 9 chars (other than line break chars, but the pattern won't match them)[NVQWST]
-N
,V
,Q
,W
,S
orT
[A-Z\s]{1,3}
- one, two or three uppercase letters or whitespace[0-9]{1,6}
- one to six digits(?:(?<=^.{8})[A-Z])?
- an optional occurrence of an uppercase ASCII letter if it is the ninth character in a string$
- end of string.Based on rules and details pulled from these links:
I've crafted a comprehensive and strict regex to validate Australian DVA numbers.
$regex = <<<REGEX
/
^
([NVQWST])
(?|
([ ANPVX])(\d{1,6})
|(
BG
|CN
|ET
|F[RW]
|G[RW]
|I[QTV]
|JA
|K[MO]
|MO
|N[FGKX]
|P[KOX]
|R[DMU]
|S[AELMORS]
|U[BS]
|YU
)(\d{1,5})
|(
(?:A(?:FG|GX|LX|R[GX])
|B(?:A[GL]|CG|G[GKX]|RX|U[GRX])
|C(?:AM|CG|HX|IX|LK|N[KSX]|ON|YP|Z[GX])
|D(?:EG|N[KX])
|E(?:G[GXY]|SX|T[KX])
|F(?:I[JX]|R[GKX])
|G(?:HA|R[EGKX])
|H(?:K[SX]|L[GKX]|UX)
|I(?:DA|ND|SR|T[GKX])
|K(?:OS|SH|UG|YA)
|L(?:AX|BX|XK)
|M(?:A[LRU]|LS|OG|TX|WI)
|N(?:BA|CG|GR|IG|RD|S[MSW]|W[GKX])
|OMG
|P(?:A[DGLMX]|C[AGRV]|H[KSX]|L[GX]|MS|S[MW]|WO)
|QAG
|R(?:DX|U[GX])
|S(?:A[GX]|CG|EG|IN|PG|UD|W[KP]|Y[GRX])
|T(?:H[KS]|R[GK]|ZA)
|U(?:AG|RX|S[GKSX])
|V(?:EX|NS)
|Y(?:EM|GX)
|ZIM
)
)(\d{1,4})
)
([A-Z]?)
$
/x
REGEX;
The first character signifies the state/territory.
N
= New South Wales (includes Austalian Capital Territory)V
= VictoriaQ
= QueenslandW
= Western AustraliaS
= South Australia (includes Northern Territory)T
= TasmaniaMy pattern intentionally uses "branch reset" capture groups so that the match array can be easily used to pad the inner "file number" with leading digits when desired.
Here is a demo with sample DVA strings, a .
If your application requires the DVAs to be zero padded to 8 or 9 characters, then this is a tighter pattern to enforce that.
Yes, I did this all on my phone.
No, I didn't type it all out maually.
I scraped the one webpage and used regex to format the content into array syntax for my lookup array.
Then I compacted the war code abbreviations into groups and character classes.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
A web framework called Skybreak was released at the end of 2011. After a couple of months, the team changed their name to Meteor. Using simple JavaScript, you can create apps that can be used across multiple platforms, including but not limited to Android and iOS. The Meteor JS framework provides full stack solutions. The use of this infrastructure includes important areas such as back-end development, database management, business logic, and front-end rendering.
https://www.meteor.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.