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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Replace string in input field dynamically

    Hi, I am trying to write some code that will test if a user enters a non-numeric character in an input text field, and if so, replaces it with a '0'

    Code:
    <script type="text/javascript">
    function IsNumVal (){
    	for (J=1; J<=3; J++){
    		if (isNaN(Number(document.getElementById('Intext' + J).value))){
    			document.getElementById('Intext' + J).value = 0;
    		}
    	}
    }
    </script>
    
    <Body>
    <table border="1" cellspacing="1" id = "Numbers">
      <tr id="RowNum1">
        <td ><input type="text" size = "5" name="Intext1" id="Intext1" value = "3" 
     onkeyup="IsNumVal()" /></td>
      </tr>
      <tr id="RowNum2">
        <td><input type="text" size = "5" name="Intext2" id="Intext2" value = "9" 
     onkeyup="IsNumVal()" /></td>
      </tr>
      <tr id="RowNum3">
        <td><input type="text" size = "5" name="Intext3" id="Intext3" value = "18"
      onkeyup="IsNumVal()" /></td>
      </tr>
    </table>
    </Body>
    document.getElementById('Intext' + J).value = 0; doesn't work
    document.Schedule.'Intext' + J.value = 0; doesn't work
    document.Schedule.Intext2.value = 0; works, but I need to test each text field while looping, so I can't "hard code" the ID.

    Suggestions?
    Last edited by javanew; 09-20-2011 at 12:06 PM.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    Code:
    <input type="text" id="Intext" onkeyup="value=IsNumVal(this.value)"></input>
    
    function IsNumVal (str){
    newstr=str.replace(/[^0-9]/g,0)
    return newstr;
    }

  • Users who have thanked xelawho for this post:

    javanew (09-20-2011)


  •  

    Posting Permissions

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