Home  |  About  | Last |  Submit  |  Contact
AllQuests.com




Previous Question:  CFWebstore experience  Just Starting Your DesignNext Question:  Guidelines For Designing School Websites UK  Just Starting Your Design
Question Need scripting help ( SitePoint Forums Just Starting Your Design )
Updated: 2008-08-12 07:20:28 (6)
Need scripting help

Hihi,

I need a script to separate a phone number into the following format:

Original Number: 1234567890

Tel[1]: 123
Tel[2]: 456
Tel[3]: 567890

Tel[3] should contain whatever remaining numbers (be it 3/4/5/more digits) there are in the number.

Pls advise..thks in advance.

Weiming

Answers: Need scripting help ( SitePoint Forums Just Starting Your Design )
Need scripting help

What language would you need it in? It can be done in various languages. I would opt for PHP but your file would need to be a .php file to work.

LiquidReflex

Need scripting help

I am using PHP for that page...so scripts for PHP will be useful for me

Thanks again!

gunbound_boy

Need scripting help

In Javascript it could be something like:
Code:
<script language="JavaScript1.5" type="text/javascript">

function telNbr(tlphnbr){
var tlphSliced = new Array();
tlphSliced[0] = tlphnbr.value.slice(0,3);
tlphSliced[1] = tlphnbr.value.slice(3,6);
tlphSliced[2] = tlphnbr.value.slice(6);
}

</script>
The slice function in Javascript does not include the last character. As an example:

String "hello world"

slice(0,4) would result in "hell", that is, characters 0, 1, 2 and 3 (remember Javascript start numbering from 0, not from 1)

in PHP, it could be something like this:

Code:
<?php $telNbr=array;
$telNbr['0']=substr($_POST['name_of_text_field'],0,2);
$telNbr['1']=substr($_POST['name_of_text_field'],3,5);
$telNbr['2']=substr($_POST['name_of_text_field'],6);
?>
If your method is not post, but get, substitute $_POST for $_GET.

The function substr does include the last character. Same example as before

substr('hello world', 0, 4) would result in "hello", that is, characters 0, 1, 2, 3 and 4


Be aware that there is not error handling here, so if someone keys something like 124 instead a full telephone number, you won't get an error but it will not work.
Hope this helps.

molona

Need scripting help

Well a basic way to do it is to use the substr function in PHP:

Let's assume that your original number is a variable called $phonenum with the value of 9876543210.

PHP Code:

<?php

// Determine the first 3 numbers
$first_num = substr($phonenum,0,3);

// Determine the next 3 numbers
$sec_num = substr($phonenum,4,3);

// Determine the rest of the numbers
$last_num = substr($phonenum,7);
?>
This will return the following variables:
$first_num = 987
$sec_num = 654
$last_num = 3210

However, this is based on the user putting in all the numbers together (no punctuation, spaces, etc). If you need to accomodate for those, there would need to be more checking and editing of the original variable first (ie, find any -, () or other marks).

Off Topic:

not fast enough I guess

LiquidReflex

Need scripting help

Thanks everyone for your help!

Greatly appreciated...

gunbound_boy

Need scripting help

Quote:
Originally Posted by LiquidReflex
Well a basic way to do it is to use the substr function in PHP:

Let's assume that your original number is a variable called $phonenum with the value of 9876543210.

PHP Code:

<?php

// Determine the first 3 numbers
$first_num = substr($phonenum,0,3);

// Determine the next 3 numbers
$sec_num = substr($phonenum,4,3);

// Determine the rest of the numbers
$last_num = substr($phonenum,7);
?>
This will return the following variables:
$first_num = 987
$sec_num = 654
$last_num = 3210

However, this is based on the user putting in all the numbers together (no punctuation, spaces, etc). If you need to accomodate for those, there would need to be more checking and editing of the original variable first (ie, find any -, () or other marks).

Off Topic:

not fast enough I guess
Well, in my example, I assumed that the text field was in a form. If it isn't, then your scripting is the appropiate one

molona

Previous Question:  CFWebstore experience  SitePoint Forums  Just Starting Your DesignNext Question:  Guidelines For Designing School Websites UK  SitePoint Forums  Just Starting Your Design

- Source: Need scripting help SitePoint Forums Just Starting Your Design
- Previous Question: CFWebstore experience SitePoint Forums Just Starting Your Design
- Next Question: Guidelines For Designing School Websites UK SitePoint Forums Just Starting Your Design