Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

spliting address 1

Status
Not open for further replies.

choudhmh

Programmer
Aug 9, 2004
34
GB
I have develop ths script where it fills addresses up.

function FillAddress(Address, type) {

//debugger ;

if ( Address == '0' || Address == 'none' ) {
if (type=='bill') {
document.frmAddressDetails.txtbilladdress1.value = '';
document.frmAddressDetails.txtbilladdress2.value = '';
document.frmAddressDetails.txtbilltown.value = '';
document.frmAddressDetails.txtbillcounty.value = '';
document.frmAddressDetails.txtbillpostcode.value = '';
document.frmAddressDetails.txtbilladdress1.focus();
} else {
document.frmAddressDetails.txtaddress1.value = '';
document.frmAddressDetails.txtaddress2 .value = '';
document.frmAddressDetails.txttown.value = '';
document.frmAddressDetails.txtcounty.value = '';
document.frmAddressDetails.txtpostcode.value = '';
document.frmAddressDetails.txtaddress1.focus();
}

} else {

var txtaddress1 = '';
var txtaddress2 = '';
var txttown = '';
var txtcounty = '';
var txtpostcode ='';

splitAddress = Address.split(",");

txtpostcode = splitAddress[splitAddress.length-1].replace(/^\s*|\s*$/,"")


txtaddress1 = splitAddress[0].replace(/^\s*|\s*$/,"");
txtaddress2 = splitAddress[1].replace(/^\s*|\s*$/,"");
txttown = splitAddress[2].replace(/^\s*|\s*$/,"");
txtcounty = splitAddress[3].replace(/^\s*|\s*$/,"");

if(txtcounty == txtpostcode) txtcounty ='';


if (type=='bill') {

document.frmAddressDetails.txtbilladdress1.value = txtaddress1;
document.frmAddressDetails.txtbilladdress2.value = txtaddress2;
document.frmAddressDetails.txtbilltown.value = txttown;
document.frmAddressDetails.txtbillcounty.value = txtcounty;
document.frmAddressDetails.txtbillpostcode.value = txtpostcode;

} else {

document.frmAddressDetails.txtaddress1.value = txtaddress1;
document.frmAddressDetails.txtaddress2.value = txtaddress2;
document.frmAddressDetails.txttown.value = txttown;
document.frmAddressDetails.txtcounty.value = txtcounty;
document.frmAddressDetails.txtpostcode.value = txtpostcode;

}
}
return;
}



but what happening i have this address where splitAddress[3] is null. What script will i need to insert so if line 3 is null, it does not add any text to the textbox.

Does any one know how i will amend the text.
Thanks
Mac
 
Maybe replace:

Code:
txtcounty = splitAddress[3].replace(/^\s*|\s*$/,"");

with this:

Code:
txtcounty = (splitAddress.length > 3) ? splitAddress[3].replace(/^\s*|\s*$/,"") : "";

Essentially, it checks to see if the array splitAddress has more than 3 elements (0, 1, 2, [blue]3[/blue]), and if it does, assigns txtcounty the value, otherwise, it assigns txtcounty an empty string.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
The above script throws an error something about this brackets is missing )
 
Sorry cLFlaVA
i've got it nearly working with the above code, onle one problem, the postcode is shown again on the txtcounty textbox - this should be blank. how would i get rid of the value of postcode from txtcounty value, if the address on split[3] is null.
 
I'm not sure I understand what you're asking...

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Using the above code you submitted. On the first textbox firstline of address is inserted, on the second line the town value is shown, but on the third textbox the postcode is shown. I have a separate textbox for postcode. with your code the postcode is now shown twice. on the original postcode textbox as well as textbox3 that the splitAddress[3]section.
How would i get rid of any text which are being shown here.

thanks
mac
 
Kind of hard to tell without some sample data. Maybe it's because of this: I've commented your code with examples:

Code:
[green]// for our example:
// splitAddress[0] is '123 Fourth Street'
// splitAddress[1] is 'PO Box 25'
// splitAddress[2] is 'Hamden'

// after this line of code, txtpostcode is 'Hamden'[/green]
txtpostcode = splitAddress[splitAddress.length-1].replace(/^\s*|\s*$/,"")

[green]// after this line of code, txtaddress1 is '123 Fourth Street'[/green]
txtaddress1 = splitAddress[0].replace(/^\s*|\s*$/,"");

[green]// after this line of code, txtaddress2 is 'PO Box 25'[/green]
txtaddress2 = splitAddress[1].replace(/^\s*|\s*$/,"");

[green]// after this line of code, txttown is 'Hamden'[/green]
txttown = splitAddress[2].replace(/^\s*|\s*$/,"");

[green]// this is my line - after this line txtcounty will be ''[/green]
txtcounty = (splitAddress.length > 3) ? splitAddress[3].replace(/^\s*|\s*$/,"") : "";

So, as you see there, txtpostcode and txttown are both 'Hamden'.

Maybe you want to change this line:

Code:
 if(txtcounty == txtpostcode) txtcounty ='';

To this:

Code:
 if(txttown == txtpostcode) txttown ='';


Does this help?

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Hi cLFlaVA

Fanatastic, its working perfectly now. Thanks for all your help. You're a genius.

Mac
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top