how do i enter only numbers in a form field?
Moderator: General Moderators
-
mikewooten
- Forum Contributor
- Posts: 169
- Joined: Wed Feb 11, 2004 12:13 pm
- Location: Duluth, Georgia
- Contact:
how do i enter only numbers in a form field?
i have a form with a few fields such as zip and phone. i only want the user to type in those fields numbers and not letters, and if the user enters letters, then the form will warn the user somehow to only enter number.
is there a function that will accomplish this or how is something like this accomplished?
what code would i have to use?
any help would be greatly appreciated.
thanks
is there a function that will accomplish this or how is something like this accomplished?
what code would i have to use?
any help would be greatly appreciated.
thanks
use a regex
PHP example:
PHP example:
Code: Select all
if(!eregi("^[_\.0-9-]+$",$_POST['zip'])){ die("Zip may only be numbers."); }on the client side as the first step you can do something like
in javascript
but stil validate on the server side as scrotaye suggests.
Code: Select all
if(isNan(document.getElementById('zip').value)
{
alert('please enter numbers only');
document.getElementById('zip').focus();
}in javascript
but stil validate on the server side as scrotaye suggests.
A purely javascript/html solution:
Code: Select all
<script language="e;Javascript"e;>
function is_numeric(formfield){
var field=formfield.value
var regEx = /\D*/g;
return field.replace(regEx,"e;"e;);
}
</script>
<input type="e;text"e; onChange="e;this.value=is_numeric(this)"e; size="e;35"e;>