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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.