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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Northern California
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    check for integers

    I have this script im working on, an I want it to searches a form and removes any characters that are not an integer but for some reason I am unable to do this?
    Code:
    <html>
    <head>
    <script>
    var check_string="";
    var ending_string="";
    function check_char(){
    	check_string=document.entry.data.value;
    	for(i=0;i<check_string.length;i++){
    		if(!isNaN(check_string.charAt(i))){
    		ending_string+=check_string.charAt(i);
    		}
    	}
    	check_string="";
    	check_string=ending_string;
    	ending_string="";
    	return false;
    }
    
    
    </script>
    </head>
    <body>
    <form name="entry" onSubmit="return check_char()">
    	<input type="text" name="data" value=""/>
    	<input type="submit" value="check"/>
    </form>
    </body>
    </html>
    Last edited by scriptkeeper; 07-14-2003 at 03:36 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,914
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You are going about this the hard way. A regular expression will solve your problem:-

    <input type="text" name="data" onblur="if(/\D/g.test(this.value)){alert('Only numbers are allowed in this box. '); this.value=''; this.focus()}">

  • #3
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Yea reg expressions are the way to go, I've been trying to get to grips with them lately.

    Instead of alerting that its not a number you can automcaticly strip non numerics

    Code:
    <input type="text" name="data" onblur='this.value=this.value.replace(/[^0-9]/g, "");'>


  •  

    Posting Permissions

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