Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accepting numbers only - if not, erase it

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have this script
Code:
	function numberOnly(val) {
		var isNumber = !isNaN(val);
		if(isNumber){
			if(val.indexOf('e') != -1){
				isNumber = false;
			}
		}
		return isNumber;
	}

The HTML is something like this
Code:
<input name='field' id='fieldID' onkeyup='return numberOnly(this.val);' />

I was hoping that by using 'return' within the onkeyup event, I would effectively negate the last key stroke.

It does not work! What is my solution?

Thanks,


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
It does not work! What is my solution?
Your solution is incomplete, that's what it is.

When that function returns false (when there is a non-validating character), then you need to update the value attribute of the form field to remove the non-validating character. So a little more work to complete the task.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I was so pressured for time that I had to use an AJAX call to a PHP script to do it :)

I truly appreciate your coming back and checking on me ... Now that the pressure cooker is off the burner, I will come back to the JS and try to finish it. I will let you know how I do later.

Regards,


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
BabyJeffy,

Here is what I came up with
Code:
function isNumber(val) {
	var strSearch = '0123456789';
	var strValue = val.value;
	var strLength = strValue.length;
	var lastChar = val.value.charAt((strLength) - 1);
	if(strSearch.indexOf(lastChar) == -1) {
		var cleanValue = val.value.substring(0, (strLength) - 1);
		val.value = cleanValue;
	}
}

Since I am not so proficient in javascript, it took some searching around and modifying a script I found ...

Thanks for the encouragement! :)



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Here is what I use to limit input to numeric entry:

Code:
<script language="javascript" type="text/javascript">
   <!--
   function isNumberKey(evt)
   {
      var charCode = (evt.which) ? evt.which : event.keyCode
      if (charCode > 31 && (charCode < 48 || charCode > 57))

         return false;

         return true;
   }
   //-->
</script>

Call this function on the onkeypress or onkeydown event of a textbox.
This obviously won't protect against copying and pasting, so make sure you have some data cleansing on the server side.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top