how do i enter only numbers in a form field?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
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?

Post by mikewooten »

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

use a regex

PHP example:

Code: Select all

if(!eregi("^[_\.0-9-]+$",$_POST['zip'])){ die("Zip may only be numbers."); }
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

on the client side as the first step you can do something like

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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

A purely javascript/html solution:

Code: Select all

<script language=&quote;Javascript&quote;>
function is_numeric(formfield){
	var field=formfield.value
	var regEx = /\D*/g;
	return	field.replace(regEx,&quote;&quote;);
}
</script>

<input type=&quote;text&quote; onChange=&quote;this.value=is_numeric(this)&quote; size=&quote;35&quote;>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think ~patrikG's solution is better than ~phpScott's, because ~patrikG's will actually remove the offending characters, whereas ~phpScott's won't
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

I agree but I gave a js solution for what was being asked.

Oh well always next time to pull the stuning answer out of my hat. :cry:
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

phpScott wrote:I agree but I gave a js solution for what was being asked.

Oh well always next time to pull the stuning answer out of my hat. :cry:
There is no next time when it comes to beer! Let's have a ~round:

Image
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

sussex is a bit far from B'ham(I think)
But after work I think I will.

Cheers mate.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

just a wee bit of a drive ;) But hey, we've earned it, the weekend is whiffable
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Not much of a head on that beer! Suppose that's how southern softies like it :lol:
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

That's the first time I heard an English guy complain about not getting enough head. 8O
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

being a canuck living in the UK phpScott quickly walks the other way to the pub and starts to ignore where this conversation is going. :arrow:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

patrikG wrote:That's the first time I heard an English guy complain about not getting enough head. 8O
:lol: :lol: :lol: :lol:

Im only grumpy cos that beer has made me crave the amber nectar! 30 mins to go until i finsih work, then ill be sat outside with one of those frosty bad boys 8)
Post Reply