# convert this function from asp to js

• Apr 20, 2011, 03:59
antonyx
convert this function from asp to js
hello, i have a function that was written out for me in asp. i am trying to convert it to js but am getting confused with the double IF syntax, can somebody assist me in converting this to javascript:

Code:

```D = "W1K 1BE" DL = Instr(D," ") Dleft = left(D,DL - 1) Dleft2 = left(Dleft,2) Dright2 = mid(Dleft,3) Dright2len = len(Dright2) i = 1 Dright2end = Dright2 Do While i <= Dright2len         strcheck = mid(Dright2,i,1)         if IsNumeric(strcheck)        then                 Dright2end = Dright2end         else                                                         strcheckexists = Instr(Dleft2,strcheck) if strcheckexists = 0 then                         Dright2end = Replace(Dright2end,strcheck,"")                 end if         end if                i = i +1 Loop strfinal = Dleft2 &  Dright2end```
thanks
• Apr 20, 2011, 18:31
paul_wilkins
It seems to be a nested if statement.

Here's how the structure should be:

Code:

```do {     ...     if (IsNumeric(strcheck)) {         ...     } else {         ...         if (strcheckexists = 0) {             ...         }     }     ... } while (i <= Dright2len);```
• Apr 20, 2011, 20:07
paul_wilkins
This code started out as a direct conversion of the script, then went through a series of reductions to end up with the following:

Code javascript:

```function convert(str) { var parts = str.split(' '), chars = parts[0].match(/\D/g).join(''), // non-numeric chars re = new RegExp('[^\\d' + chars + ']', 'g'); // anything that is not (a digit or one of the chars) return parts[0] + parts[1].replace(re, ''); }   var converted = convert('W1K 1BE'); // converted is 'W1K1'```

The end result is the same, where any letters in the second part are removed if they're not in the first part.
• May 10, 2011, 06:54
antonyx
hello, thank you very much for that response. i still have not got this working and am becoming very confused. i think the way my page has been coded is that the javascript is held within the asp file...

here is my code (i have removed a lot of surplus code)

Code:

```<%@ Language = "JavaScript" %> <% Response.ContentType = "text/html"; var P = ""; if (Request.Form("P").Count == 1) {     P = String(Request.Form("P")); } else if (Request.QueryString("P").Count == 1) {     P = String(Request.QueryString("P")); } %> <h1><% Response.Write(P); %> to Heathrow Airport</h1>```
i have highlighted in bold where my postcode displays. the value that i am trying to manipulate is P

what i need help with now is integrating that function into this code and i would also like to clarify the rules again of the manipulation...

NW1A 1AA

first rule - remove all values after and including the space

NW1A

second rule - remove all alphabetical characters that are not in the first 2 characters of the value

NW1

that's it. i would need this rule to be applied to my P value and if someone can match the syntax of my current code i shall try it out.

thanks again and look forward to a response
• May 10, 2011, 07:52
paul_wilkins
Quote:

Originally Posted by antonyx
what i need help with now is integrating that function into this code and i would also like to clarify the rules again of the manipulation...

NW1A 1AA

first rule - remove all values after and including the space

NW1A

second rule - remove all alphabetical characters that are not in the first 2 characters of the value

NW1

that's it.

Oh well thats good. Now that we have the rules we can create a regular expression that does the work for us.

The two letters could be either together or apart.

If as I understand it, these are UK post codes, they won't start with a number, so the worst case could be A34B56. We can capture that using a regular expression where the numbers on either side can be optional too.

So [A-Z]\d*[A-Z]\d* is the regular expression that gets a letter followed by 0 or more digits, then another letter followed by 0 or more digits.

However, as these are UK postcodes, you should be able to get away with matching one or more consecutive digits, followed by one or more digits

[A-Z]+\d+

So we can put that within parenthesis as a capture group, and place a carat (^) at the start to ensure that the matches start from the beginning.

^([A-Z]+\d+)

Finally, we put forward slashes on either side, and that's your regular expression.

Code javascript:

`var rx = /(^[A-Z]+\d+)/;`

Let's test it out.

Code javascript:

```var postcode = 'NW1A 1AA'; var rx = /(^[A-Z]+\d+)/; var result = rx.exec(postcode); area = result[1]; // area is 'NW1'```

So as a javascript function that would be:

Code javascript:

```function postcodeArea(postcode) { var rx = /(^[A-Z]+\d+)/; var result = rx.exec(postcode); return result[1]; }```

The code in your example is all ASP code, which all runs before the result is sent provided as a web page.
JavaScript on the other hand runs on the client side after the web page has been sent.

You should find though that the regular expression can be used in the same way, it's just the implementation details of ASP which differ.
• May 10, 2011, 08:13
antonyx
hi again, i am using the following code:

Code:

```<% var postcode = P var rx = /(^\d*[A-Z]\d*[A-Z]\d*)/; var result = rx.exec(postcode); area = result[1]; %> <h1><% Response.Write(area); %> to Heathrow Airport</h1>```
and am getting this error:

Microsoft JScript runtime error '800a138f'

'result.1' is null or not an object
• May 10, 2011, 15:57
paul_wilkins
Quote:

Originally Posted by antonyx
hi again, i am using the following code:

Code:

```<% var postcode = P var rx = /(^\d*[A-Z]\d*[A-Z]\d*)/; var result = rx.exec(postcode); area = result[1]; %> <h1><% Response.Write(area); %> to Heathrow Airport</h1>```
and am getting this error:

Microsoft JScript runtime error '800a138f'

'result.1' is null or not an object

This is the JavaScript forum, and I don't know the first thing about ASP.

Do you want to move this thread to our Classic ASP forum?