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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

allow only numerical values to be entered in a textbox

Status
Not open for further replies.

namax

Technical User
May 22, 2011
19
PG
I have created a php form and addaded a text box(name="preidno") to enter id numbers and would like to let data entered to be only numbers.

Could anyone help me with this problem please since I am new to php programming?

Thankyou in advance.
 
It is not possible to block all non-numeric input from the client, but you can use javascript to do validation before anything is submitted, if the client allows and supports it. You'd better ask in a javascript forum.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Hi

In addition to DonQuichote's reply, the JavaScript check is just a guidance for the users. Validation must be done with server-side coding :
PHP:
[teal]<?php[/teal]
[b]if[/b] [teal](![/teal][COLOR=darkgoldenrod]is_numeric[/color][teal]([/teal][navy]$_GET[/navy][teal][[/teal][green][i]'preidno'[/i][/green][teal]])[/teal] [b]die[/b][teal]([/teal][green][i]'go back and write numbers only in the id field'[/i][/green][teal]);[/teal]
Note that you wrote "only numbers", so I used [tt]is_numeric()[/tt] to check for numbers. See its documentation for details on what kind of numbers it accepts and what alternatives exist.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top