1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How To Clear A Text Window?

Discussion in 'HTML & JavaScript' started by The Scarlet Pimp, Jun 11, 2008.

  1. The Scarlet Pimp

    The Scarlet Pimp Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 2, 2008
    Messages:
    788
    Likes Received:
    3,127
    Occupation:
    Chair moistener.
    Location:
    Cyberspace
    I'm using this code to make sure that only numbers are added. If letters are typed into the text window then an alert box pops up. But, here's the problem, it doesn't erase the letters.

    This means that someone can still send letters rather than numbers, just by clicking the SUBMIT button. How do I make this script delete the text in the window after the alert box is closed?

    ---

    <form>
    <input type="text" value="" name="phone" onBlur="if (! this.value.match(/^[0-9ext()-]+$/)) {alert('Phone number must use only these characters: 0-9 ( ) e x t');}" onfocus="this.value=''; this.onfocus=null">
    </form>

    ---

    tsp
     
  2. The Scarlet Pimp

    The Scarlet Pimp Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 2, 2008
    Messages:
    788
    Likes Received:
    3,127
    Occupation:
    Chair moistener.
    Location:
    Cyberspace
    Is it possible to make a text window erase letters AS they're typed? Rather than opening an alert that says to enter numbers?

    This would be better... :D

    tsp
     
  3. anonymous

    anonymous Newbie

    Joined:
    Dec 17, 2007
    Messages:
    32
    Likes Received:
    0
    Occupation:
    Slave Trader
    AS they're typing, it would be tricky due to incompability of some browsers.(you can use some js libraries developed to pass this prob.)
    but if u want after fix:

    Code:
    <html>
    <body>
    <form>
    <script>
    function duz(id){
    var input = document.getElementById(id);
    var val = input.value;
    var len = val.length;
    var arr = "";
    	for(var a = 0;a < len;a++)
    	{
    		if(val.charAt(a).match(/^[0-9ext()-]+$/))
    		{
    		arr = arr + val.charAt(a);
    		}
    	}
    input.value = arr;
    }
    
    
    </script>
    <input type="text" value="" name="phone" id="idofinput" onBlur="duz('idofinput');" onfocus="this.value=''; this.onfocus=null">
    
    </form>
    </body>
    </html>
    
    
    NOTE: Your regular expression is wrong. It misses some characters such as "e"
    And it is good to check your inputs in server side, client side can be manipulated
     
  4. The Scarlet Pimp

    The Scarlet Pimp Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 2, 2008
    Messages:
    788
    Likes Received:
    3,127
    Occupation:
    Chair moistener.
    Location:
    Cyberspace
    1. thanx for the script! :D

    2. i was thinking the same thing... i'd love to do this in php. since js can be easily disabled by turning it off in the web browser... :confused:


    tsp
     
  5. jakcharles

    jakcharles Registered Member

    Joined:
    Jun 10, 2008
    Messages:
    72
    Likes Received:
    0
    can i use this script?
    if yes then tell me the way how i can use
     
  6. anonymous

    anonymous Newbie

    Joined:
    Dec 17, 2007
    Messages:
    32
    Likes Received:
    0
    Occupation:
    Slave Trader
    u can.
    c/p js part and add id,onblur parts into your input
     
  7. The Scarlet Pimp

    The Scarlet Pimp Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 2, 2008
    Messages:
    788
    Likes Received:
    3,127
    Occupation:
    Chair moistener.
    Location:
    Cyberspace
    your script is a bit better than mine... :D

    the cool part is even if someone c&p info into the text window,
    as soon as they move to another text window the data clears.

    love messing around with scripts!!! :D

    tsp