Jump to content

Only allowing numbers and decimals


Smudly

Recommended Posts

I'm working on only allowing numbers & decimals into text fields. So far, I have it deleting Letters, but it really should never allow letters to be typed in in the first place. It is also deleting decimal points, which is not what I need it to do.

How do I prevent typing in letters altogether using this code?

 

see example page here: click here

The top field is the one we're working with right now.

Here is a snippet of my code:

 

	
creditsBanner = parseFloat(document.convertform.creditsbanner.value);

creditsBalance = "<?php echo $creditsBalance;?>";

if (!isNaN(creditsBanner)) {

	if(creditsBanner>creditsBalance){
		document.convertform.creditsbanner.value = creditsBalance;
		creditsBanner = creditsBalance;
	}
	document.convertform.creditsbanner.value = creditsBanner;
}
else{
	alert ("Only numbers are allowed in this field");
	document.convertform.creditsbanner.value = "";
}

Link to comment
https://forums.phpfreaks.com/topic/233508-only-allowing-numbers-and-decimals/
Share on other sites

<form name='convertform' action='..' method='..'>
  <input type='text' name='creditsbanner' onkeypress='return validateField(event)'>
</form>

<script type='text/javascript'>
function validateField(e) {
  return !(/[^0-9.]/.test(String.fromCharCode((window.event)?e.keyCode:e.which)));
}
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.