What's really happening here is that your server-side code is swapping in the value where you've put <%= sPostalCode %>, and sending the result to the browser. So if the sPostalCode were (say) "NW10", you'd get
window.onload function PostCodeChecker() {
if (NW10 !== 'SW') {
// ^--- ERROR (unless you happen to have a client-side variable by that name, which I'm guessing you don't)
alert("This is not an SW Postal Code");
}
}
So you'll need quotes around it (and you'll need to add the missing =), like this:
// v-- was missing
window.onload = function PostCodeChecker() {
if ('<%= sPostalCode %>' !== 'SW') {
// ^--- here ^--- and here
alert("This is not an SW Postal Code");
}
}
...so that what goes to the browser is:
window.onload = function PostCodeChecker() {
if ('NW10' !== 'SW') {
alert("This is not an SW Postal Code");
}
}
That assumes that the string won't have any ' characters in it, or other characters that would result (when the server-side processing is done and the result sent to the browser) in an invalid JavaScript string literal.
Of course, you could just do this:
<% If sPostalCode <> "SW" Then %>
window.onload = function() {
alert("This is not an SW Postal Code");
}
<% End If %>
...since it's a server-side variable, you can do the test server-side.
Side note: This form of expression:
something = function name() { ... };
...has issues on various browsers, most notably IE. It's called a named function expression because it's a function expression where the function has a name (name, in this case). On IE it results in two completely separate functions being created, which can be ever so confusing. More: Double-take
<%= ... %>.