Regular Expressions
Discussion
Thanks. I've read all kinds of guides but I still can't get my head around it. The requirements look simple on the surface, but one you start digging into it, it's far from simple - unless I'm being particularly thick
Edit: I'm not entirely sure a regex is going to do the job by itself anyhow as I need to know the numeric value following the start of the postcode, ie NG24 falls into a certain range...
>> Edited by judas on Thursday 2nd June 12:04
Edit: I'm not entirely sure a regex is going to do the job by itself anyhow as I need to know the numeric value following the start of the postcode, ie NG24 falls into a certain range...
>> Edited by judas on Thursday 2nd June 12:04
Nah - you're right. Its simple to match the letters "NG" followed by 1 or 2 digits, but adding the constraints to tie the digits down to the allowable combinations that you're after is a bit tricker.
You could, of course, simply list all the allowable values but there must be a more elegant solution.
{edited to add:} Been thinking about it for a bit and its made my head hurt. I'd be tempted to code it rather than regex it, unless somebody else can knock up a simple pattern that would do the job...
>> Edited by pdV6 on Thursday 2nd June 12:11
You could, of course, simply list all the allowable values but there must be a more elegant solution.
{edited to add:} Been thinking about it for a bit and its made my head hurt. I'd be tempted to code it rather than regex it, unless somebody else can knock up a simple pattern that would do the job...
>> Edited by pdV6 on Thursday 2nd June 12:11
This will match any valid UK postcode:
/^([A-Za-z]{1,2})([0-9]{1,2})( {0,1})([0-9])([A-Za-z]{2})$/
... so to make it NG-specific:
/^NG([0-9]{1,2})( {0,1})([0-9])([A-Za-z]{2})$/
If you then pull out the first group you get the NG sub-area and you can do a numeric test to see if it's >=1 and <= 24
/^([A-Za-z]{1,2})([0-9]{1,2})( {0,1})([0-9])([A-Za-z]{2})$/
... so to make it NG-specific:
/^NG([0-9]{1,2})( {0,1})([0-9])([A-Za-z]{2})$/
If you then pull out the first group you get the NG sub-area and you can do a numeric test to see if it's >=1 and <= 24
rebelstar said:
If you then pull out the first group you get the NG sub-area and you can do a numeric test to see if it's >=1 and <= 24
That's the bit that was making my head hurt in the expression. Not sure its actually possible except by providing an exhaustive list of what you consider to be valid numbers. So, a bit of regex and code seems to be the way forward!
rebelstar said:
This will match any valid UK postcode:
/^([A-Za-z]{1,2})([0-9]{1,2})( {0,1})([0-9])([A-Za-z]{2})$/
Be careful with relying on this, though, to validate postcodes as obviously there are more possible allowable postcodes than actual codes. Also, there are certain combinations that are disallowed even though they fit the usual patterns.
I've had to write acres of postcode validation code for some applications. A right PITA I can tell you...
rebelstar said:
I've got a fully working HTML test page to prove it all out (including the 1->24 test). Let me know if you want it and I'll email it via your profile.
I *think* I've got it sorted now - still very clumsy though. Validating server-side with a VBScript function. Still, a copy of your code would be handy
Here's the function - Ted's code strips out the formatting so it's a bit had to read:
Function CheckPostcode(Postcode)
on error resume next
Set objRegExp = New RegExp
With objRegExp
.IgnoreCase = true
.Pattern = "^NG([0-9]{1,2})(s)([0-9])([A-Za-z]{2})$"
.Global = true
If .Test(Postcode) = true Then
'Valid postcode
arrPostcode = Split(Postcode)
Postcode = arrPostcode(0)
.Pattern = "^NG([0-9]{1,2})$"
If .Test(Postcode) = true Then
For x = 1 to Len(Postcode)
TempStr = Mid(Postcode,x,1)
TempStr = CInt(TempStr)
If isNumeric(TempStr) Then
PCNumbers = PCNumbers & TempStr
End If
Next
PCNumbers = CInt(PCNumbers)
If (PCNumbers > 0) and (PCNumbers < 25) Then
MM_abortEdit = false
Else
MM_abortEdit = true
ErrorText = "The postcode is not valid for online orders. Please contact xxxxxx directly on the above number to place your order."
End If
Else
MM_abortEdit = true
ErrorText = "The postcode is not valid for online orders. Please contact xxxxxxx directly on the above number to place your order."
End If
Else
'Invalid postcode
MM_abortEdit = true
ErrorText = "The postcode is not valid for online orders. Please contact xxxxxxx directly on the above number to place your order."
End If
End With
End Function
>> Edited by judas on Thursday 2nd June 12:57
How about (in JavaScript, but shouldn't be too hard to switch to VBScript if you want to limit your audience
):
function postcode(pc) {
var regexp = new RegExp("^NG([0-9]{1,2})( )?([0-9])([A-Za-z]{2})$", "i" );
var matches = regexp.exec(pc);
if ( matches && matches[1] > 0 && matches[1] < 25 ) {
alert('Okay');
} else {
alert('Sorry');
}
}
>> Edited by anonymous-user on Thursday 2nd June 17:33
>> Edited by anonymous-user on Thursday 2nd June 17:34
): function postcode(pc) {
var regexp = new RegExp("^NG([0-9]{1,2})( )?([0-9])([A-Za-z]{2})$", "i" );
var matches = regexp.exec(pc);
if ( matches && matches[1] > 0 && matches[1] < 25 ) {
alert('Okay');
} else {
alert('Sorry');
}
}
>> Edited by anonymous-user on Thursday 2nd June 17:33
>> Edited by anonymous-user on Thursday 2nd June 17:34
Your code was cleaner and more compact, and changing the ( {0,1}) to ( ?) was something I should have done. In my defense, I've never done much with javascript before!
One possible improvement though: if you're using the "i" flag, doesn't that mean case-insensitive? If that's true, you can shorten the regexp slightly (an exercise for the reader).
One possible improvement though: if you're using the "i" flag, doesn't that mean case-insensitive? If that's true, you can shorten the regexp slightly (an exercise for the reader).
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff




