Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    60
    Thanks
    14
    Thanked 0 Times in 0 Posts

    a statement to output the reformated number to the form

    Hi does anyone know how to output a reformated number to a form?? This is what I have so far:

    Code:
    if (!document.userSurvey.phone.value)
        {
            alert("Phone number missing. Please enter a valid phone number to continue.");
            document.userSurvey.phone.focus();
            return false;	  
        }
        else
        {
            var numbersOnly = "";
            var chars = "";
            var phoneNo = document.userSurvey.phone.value;
    
            for (i = 0; i < phoneNo.length; i++)
            {
                chars = phoneNo.substring(i,i+1);
              
                if (chars >= "0" && chars <= "9")
                {
                    numbersOnly = numbersOnly + chars;
                }
            }
    
            if (numbersOnly.length != 13)
            {
                alert("Incorrect phone number format.You must enter 13 numbers.");
                document.userSurvey.focus();
                return false;
            }
            else
            {
                var areacode = numbers.substring(0,2);
                var leading0 = numbersOnly.substring(2,3);
                var exchange = numbersOnly.substring(3,5);
                var ext1 = numbersOnly.substring(5,9);
                var ext2 = numbersOnly.substring(9);
                var newNumber =( "+" + areacode + " " +"(" + leading0 + ")" +       exchange + " " + ext1 + "-" + ext2);

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Code:
    document.userSurvey.phone.value = reformattedNumber;

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 12-15-2011 at 12:38 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    helen11 (12-15-2011)

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    60
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Do you know a statement to output userName, email, phoneNo, and book choice to the survey textarea?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Rather than write the data to a <textarea> you might prefer to put it in a <div>. A newline in a textarea is \n, a newline in a div is <br>


    Code:
    <textarea id ="txtarea1" rows = "6" cols = "30"></textarea>
    <br>
    <div id = "div1"></div>
    <script type = "text/javascript">
    
    var userName= "Mickey Mouse";
    var email = "Mickey@nowhere.com";
    var phone = "123-456-7890";
    var book = "Karma Sutra";
    
    var combined = userName + "\n" + email + "\n" + phone + "\n" + book + "\n";
    document.getElementById("txtarea1").value = combined; 
    var combined2 = userName + "<br>" + email + "<br>"+ phone + "<br>" + book + "<br>";
    document.getElementById("div1").innerHTML = combined2;
    </script>
    Last edited by Philip M; 12-15-2011 at 01:05 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •