Click to See Complete Forum and Search --> : working with strings


geuis
08-17-2003, 02:14 PM
I have a page I'm writing where the user inputs a phone number into one field. There are 2 hidden inputs that eventually have to hold the area code and the phone prefix.
How can I split the string input into two variables so I can set them into the hidden input's values?

The code, as it is now, follows:

<html>

<body>

<FORM ENCTYPE="multipart/form-data" ACTION="http://www.dslreports.com/coinfo/information/kb" METHOD="POST">

<b>Find </b> - :
<input size=6 maxlength=6 value="">
<input type="hidden" name=npa size=3 maxlength=3 value="">-
<input type="hidden" name=nxx size=3 maxlength=3 value="">-XXXX

<INPUT TYPE="submit" VALUE="Start" name="start">

</form>

</body>

</html>

Khalid Ali
08-17-2003, 02:24 PM
JavaScript String has a function called split(argument)

suppose you have value in a text field

var val = "this is a cool forum";

now you want to split this string where the spaces are or split the string for words
var arr = val.split(" ");

the above will create an array of words in the string above separated by an empty space.
If you know the criteria to split the string you can use this function..

geuis
08-17-2003, 02:46 PM
Thanks Khalid. Under different circumstances, the split() function would work very well(I plan on using it in the future.)
Sadly, the phone number will be entered into the single input box like "5554446666" with nothing that separates the different values I need.
Is there a function that acts like the mid() function in Visual Basic? If you're not familiar with that function, basically its in the format:
mid(SourceString, Starting Integer, Amount of text to grab)

If I were to use the mid() function on a phone number, it would be like:

areaCode = mid(sourceString, 1, 3)
preFix = mid(sourceString, 3, 3)
lastFour = mid(sourceString, 6, 4)

Is there anything similar in javascript I can do?

Khalid Ali
08-17-2003, 02:52 PM
Yes I am familiar with mid().
There are multiple options for you you can use

string.substring(start,end)

or you can run a loop through the string and then get the first 3chars in one string and remainder in other string...