473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Address Validation

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
8 1932
gits
5,390 Expert Mod 4TB
hi ...

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

kind regards
Feb 4 '08 #2
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
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
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
cool ... glad to hear you got it working ... post in the forum anytime you have more questions :)

kind regards
Feb 5 '08 #9

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

Similar topics

25
by: Dynamo | last post by:
Hi The following script was taken from John Coggeshall's (PHP consultant) in his article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php // Get the email address to validate...
2
by: Angel | last post by:
Hi there, I need help pls..I'm using c# on a webform and I have this regex validation for email addresses: ValidationExpression ="\w+(\w+)*@\w+(\w+)*\.\w+(\w+)* " I'm having isssues when submit...
1
by: gotFun | last post by:
I am unable to set focus to an IP address control residing on a Property Page, following validation of the IP when the user hits the 'Finish' button on the Property Sheet. I've unsuccessfully...
3
by: Wm. Scott Miller | last post by:
Hello all: I'd like some advice on the best way to validate and confirm an e-mail address entered during a registration process. What we are thinking of is something like the following: 1. ...
35
by: Mika M | last post by:
Simple question: Does Framework (1.1) contain any routine to check entered email-address is valid ? It's quite easy to make own code for that purpose, but why to do if Framework (1.1) contain...
1
by: cemcat | last post by:
Hello, We have an ASP.NET 2.0 (C#) web form that contains a textbox for users to enter multiple e-mail addresses separated by semicolons. We need to validate that each individual e-mail address...
8
by: prabhuram.k | last post by:
Can anybody know how to validate IPV4 and IPV6 address in C++
0
by: Fur Data | last post by:
Hello, We are developing and providing the virtual market with an address validation add-on for 18 countries. A realtime prototype website can be visit at www.address-validation.com Via a...
2
by: Keith G Hicks | last post by:
I'm using the following code to send out email messages to a list of people in a database. My problem is that if I'm sending to 100 people and the 40th address is bad, it crashes on that one and...
3
by: John Wright | last post by:
I want to validate an email address against our Exchange server. Anyone have code or point me in the right direction? John
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.