By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

Address Validation

P: 36
Hi All,

I'm creating a form that it will parse a valid address in the address1 field from an address library to make sure this is a valid address. Here is the problem: The address is followed by the city and zip code inside of my address1 field. It looks like this:
Address 1: 10142 sterling ter. Rockville, 20850

I need to clean the 2 tabs between the data of the address1 and Rockville, 20850 using javascript. I'll appreciate if you could help me.
Feb 4 '08 #1
Share this Question
Share on Google+
8 Replies


gits
Expert Mod 5K+
P: 5,353
hi ...

tabs? could you give an example of the output you need?

kind regards
Feb 4 '08 #2

P: 36
Sorry, looks like the space beteween the address1 and Rockville doesn't show in the first message.

It's like this: 10142 sterling ter "space" " space" Rockille 20850.

space is as big as a tab.
Feb 4 '08 #3

P: 36
Sorry, looks like the space beteween the address1 and Rockville doesn't show in the first message.

It's like this: 10142 sterling ter "space" " space" Rockille 20850.

space is as big as a tab.
The output I need is: 10142 sterling ter.
Feb 4 '08 #4

gits
Expert Mod 5K+
P: 5,353
hi ... :)

have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. var s = '10142 sterling ter. Rockville, 20850';
  2.  
  3. s = s.match(/([^ \t]+)/g);
  4.  
  5. s = s.splice(0, 3).join(' ');
  6.  
  7. alert(s);
  8.  
we may use a regExp here to match the words only and no spaces and tabs first. next we 're-join' the first 3 elements of the result-array that we got before.

kind regards
Feb 5 '08 #5

P: 36
Hi gits,
Thank you so much for your suggestion. Please take a look at the whole javascript below:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.  
  3. var url1="http://zip4.usps.com/zip4/zcl_0_results.jsp?visited=1&pagenumber=0&firmname=&address2="; 
  4.  
  5. // 2 Broadway
  6.  
  7. var url2="&address1=&city="; //NEW YORK
  8.  
  9. var url3="&state=";//NY
  10.  
  11. var url4="&urbanization=&zip5=";
  12.  
  13.  
  14.  
  15. function getAddress( address1, city, state)
  16.  
  17. {
  18.  
  19. if(address1=="" || city=="" || state =="")
  20.  
  21. {
  22.  
  23.  alert('fill in all fields.');
  24.  
  25.  
  26.  return;
  27.  
  28. }
  29.  
  30. var fullurl=url1+address1+url2+city+url3+state+url4;
  31.  
  32. var x=createXMLHttp();
  33.  
  34. x.open("GET",fullurl, false);
  35.  
  36. x.Send(null);
  37.  
  38. var res=x.responseText;
  39.  
  40. try{
  41.  
  42. // strip off everything before where the result starts
  43.  
  44. var startpos = res.indexOf("<td headers=\"full\"")+124;
  45.  
  46. res=res.substring(startpos)
  47.  
  48. if(res.toUpperCase().indexOf("<HTML")>0)
  49.  
  50. {
  51.  
  52. alert('Address is not valid.');
  53.  
  54. return;
  55.  
  56. }
  57.  
  58. // strip off everything after the result
  59.  
  60. var endpos=res.indexOf("</td>")-10;
  61.  
  62. res=res.substring(0,endpos);
  63.  
  64. //clean up line breaks
  65.  
  66. res=res.replace("<br />","");
  67.  
  68. res=res.replace("<br/>","");
  69.  
  70. res=res.toUpperCase();
  71.  
  72. // clean off HTML Spaces
  73.  
  74. res=res.replace("&NBSP;", " ");
  75.  
  76. res=res.replace("&NBSP;", " ");
  77.  
  78. res=res.replace("&NBSP;", " ");
  79.  
  80. res=res.replace("&NBSP;", " ");
  81.  
  82. <!---  res=res.replace("res.match("/t")[a-zA-Z]*", " ");  --->
  83.  
  84.  
  85. var ad = 'address1';
  86.  
  87. ad = ad.match(/([^ \t]+)/g);
  88.  
  89. ad = ad.splice(0, 3).join(' ');
  90.  
  91. <!--- alert(ad); --->
  92.  
  93.  
  94. document.getElementById('address1').value=res;
  95.  
  96. }
  97.  
  98. catch(e)
  99.  
  100. {
  101.  
  102. alert('Address is not valid.');
  103.  
  104. }
  105.  
  106. }
  107.  
It get the full url as address and then it check the validity of the address, if not valid, it will replace the valid address in the address1 field. but the city and zipcode are added to the address and I don't want them. As you see I included your suggestion in the javascript as well. It clean the city and zip but the address validation doesn't work anymore. do you know why? Thank you so much for your support.
Feb 5 '08 #6

gits
Expert Mod 5K+
P: 5,353
actually the var ad is a string 'address1' and so the code will do nothing and you assign the res to the textfields value without stripping the tabs and spaces? ad should be res i think :)
Feb 5 '08 #7

P: 36
Doa... Thank you so much. Here is the changes I made.


<!---var res = 'address1'; --->

res = res.match(/([^ \t]+)/g);

res = res.splice(0, 3).join(' ');

<!--- alert(ad); --->


document.getElementById('address1').value=res;

I think I got it.
Feb 5 '08 #8

gits
Expert Mod 5K+
P: 5,353
cool ... glad to hear you got it working ... post in the forum anytime you have more questions :)

kind regards
Feb 5 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.