Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    resetting a text box value

    is there a way to get a text box value in a form, apply formatting to it and redisplay it before submit? for example, if you input

    5551234567 in a phone field, can you inspect it and return
    (555) 123-4567 either on the form itself or in the POST data stream?

    I have tried .htc and decided it's not suitably browser-compatible for my needs. .htc works fine if everyone's in IE but that's too restrictive.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: resetting a text box value

    That sounds feasible, as part of your validation code, inserting new pieces into a string. You could assign the final result to a hidden field to pass it to your action page, or write it back into the textbox. (If the textbox has size limits, I don't know if the script can override them or not.) I don't think you want to write directly to a POST stream; that amost certainly is beyond the reach of scripting.

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: resetting a text box value

    i think it would be best to re-write it to the textbox, and it'll get picked up as a value to pass in POST automatically.

    the question is...how?? <img src=/S/aussie.gif border=0 alt=aussie width=21 height=22>

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: resetting a text box value

    Here's a demo that takes a plain 7 or 10 character string and produces a U.S.-format phone number. It does not do any validity checking other than string length.

    <html>
    <head><title>Change Textbox Contents Demo</title></head>
    <script>
    function formatAsPhone(strID)
    {
    var phFld = document.getElementById(strID);
    var phTxt = phFld.value
    switch(phTxt.length) {
    case 7:
    phFld.value = phTxt.substring(0, 3) + "-" + phTxt.substring(3, 7);
    break;
    case 10:
    phFld.value = "(" + phTxt.substring(0, 3) + ") " + phTxt.substring(3, 6) + "-" + phTxt.substring(6, 10);
    break;
    default:
    alert("Invalid phone number length. Try again.");
    break;
    }
    }
    </script>
    <body>
    <h3>Enter a phone number and click the button</h3>
    <form name="myForm">
    Phone Number: <input type="text" name="phone" id="txtPhone"> <input
    type="button" onclick="formatAsPhone('txtPhone')" value="Format Phone Number">
    </form>
    </html>

    You'll need to incorporate it into your submit so that it returns false if the string is invalid.

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: resetting a text box value

    hmmm....ok, you set the textbox to <font face="Georgia">phField.value = [whatever]</font face=georgia>, you pass the field to the function as the id of the textbox. I'm trying to do the function with <font face="Georgia">onblur()</font face=georgia> and <font face="Georgia">onclick()</font face=georgia> rather than wait for the good ol' submit button. Well, i'll work on it tomorrow. looks good tho! <img src=/S/brainwash.gif border=0 alt=brainwash width=15 height=15>

Posting Permissions

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