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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript form validation - auto remove chars

    Im having an issue with Javascript form Validation

    I have a form where a use inputs there phone number
    I then submit that do a database, but I dont want spaces hyphens or anything other then a number (0-9)


    I have seen it before, but cant find it
    Basiclly all I want it to do, is remove any character from the input box other then numbers




    I forgot to add
    I want it to automaticly remove the characters after they (blur) unfocus from the input field

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Remove everything that is not a digit from the field's value.

    Code:
    inputelement.onchange= function(e){
      e=window.event? event.srcElement || e.target;
      e.value=e.value.replace(/\D/g,'');
    }

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks!!!!!!!!!

    Code:
     
    function onlynums(e){
      e.value=e.value.replace(/\D/g,'');
    }
     
    <input name="nums" onchange="onlynums(nums);" />


    I couldnt get it to work with the top string you included
    nor was able to understand that part

    I new it would be rather simple, I just dont understand the Operators for the replace function

    like I have no idea what /\D/g means, well i kinda do

    http://us2.php.net/preg-replace

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    \d any digit (0-9)

    \D anything not a digit

    the g means replace all matches in the string (global replace)

  • #5
    Regular Coder
    Join Date
    Dec 2005
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mrhoo View Post
    \d any digit (0-9)

    \D anything not a digit

    the g means replace all matches in the string (global replace)

    You have no idea how much that actually helps, somehow I can completely understand the replace function.


    now, Id like to kick this up a notch if possible.

    Is there any way, that if they put letters like (abc) that its converted into the number as if they push A on the phone.
    BUT instead of it automaticly doing so
    Is there a way where it would prompts
    (Letters have been detected, click ok if you would like it converted to a number,, click cancel to remove

    abc (2)
    def (3)
    ghi (4)
    jkl (5)
    mno (6)
    pqrs (7)
    tuv (8)
    wxyz (9)

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    // You can do this. One way is to set up a key function that translates any
    // sequence of numbers and letters to their 'telephone' codes.

    Code:
    var phonecode= function(str){
    	var key= {
    		a:2,b:2,c:2,d:3,e:3,f:3,g:4,h:4,i:4,j:5,k:5,l:5,
    		m:6,n:6,o:6,p:7,q:7,r:7,s:7,t:8,u:8,v:8,w:9,x:9,y:9,z:9
    	}
    	str= str.toLowerCase().replace(/[a-z]/g,function(c){
    		return key[c];
    	})
    	return str;
    }
    //test
    alert(phonecode('javascript'))

  • #7
    Regular Coder
    Join Date
    Dec 2005
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you greatly for your help. One last questions
    Is the following correct, and can it be short'en

    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    <!--
    function 
    onlynums(e){
      
    e.value=e.value.replace(/D/g,'');
    }
    var 
    phonecode= function(str){
     var 
    key= {
      
    a:2,b:2,c:2,d:3,e:3,f:3,g:4,h:4,i:4,j:5,k:5,l:5,
      
    m:6,n:6,o:6,p:7,q:7,r:7,s:7,t:8,u:8,v:8,w:9,x:9,y:9,z:9
     
    }
     
    strstr.toLowerCase().replace(/[a-z]/g,function(c){
      return 
    key[c];
     })
     return 
    str;
     
    }
    function 
    letstonums(e) {
     var 
    key= {
      
    a:2,b:2,c:2,d:3,e:3,f:3,g:4,h:4,i:4,j:5,k:5,l:5,
      
    m:6,n:6,o:6,p:7,q:7,r:7,s:7,t:8,u:8,v:8,w:9,x:9,y:9,z:9
     
    }
     
    e.value=e.value.toLowerCase().replace(/[a-z]/g,function(c){
      return 
    key[c];
     })
    }
    function 
    confirmation(e) {
     var 
    answer confirm("Alphanumeric characters detected, by default they will be converted into vanity numbers.\n Choose cancel if you would like to remove the Alphanumeric characters instead of converting.")
     if (
    answer){
      
    alert("The Alphanumeric characters have been converted into vanity numbers")
      
    letstonums(e);
      
    onlynums(e);
     }
     else{
      
    alert("Removing the Alphanumeric characters")
      
    onlynums(e);
     }
    }
    //-->
    </script>
    </head>
    <body>
    <form>
    Enter text into the box, and when you unfocus it will prompt to convert them into Vanity Numbers.<br />
    <!-- <input name="nums" onchange="alert(phonecode(nums.value));" /> -->
    <input name="lets" type="text" onblur="confirmation(lets)">
    </form>
    </body>
    </html> 

  • #8
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a similar issue. I need the form to remove txt and dashes. i only need numbers. i was able to figure out how to remove dashes.

    <input type="text" onchange="this.value = this.value.replace (/\W/g, '');">

    how do i remove standard text too? I only want numbers? Thanks!

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    <input type="text" onchange= "this.value = this.value.replace (/[^0-9]/g, '')">

    i.e. remove anything which is not 0-9

    or to "remove at source":-

    <input type="text" onkeyup = "this.value = this.value.replace (/[^0-9]/g, '')">



    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 02-03-2009 at 09:16 AM.


  •  

    Posting Permissions

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