Jump to content

PHP MySQL Forms - Only allow numbers in a textfield


Stalingrad

Recommended Posts

Hey again guys! I'm back already today. XD Okay, so... I'm building a script, right. I have a form that is a simple text field. I do NOT want to use javascript. I want to be able to use PHP to be able to do this. Here is the form exactly as it is now:

 

<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
<input type="text" name="price">

 

How would I only allow a number, and up to 5 digits long, like, for example, a zip code. I know I can always limit the char space, but they might be able to "inject" it?

 

On another note for forms, what would I have to do to get rid of people being able to run a script through a form; is it the strip_tags(); function?

Thanks for your help in advance guys!

Link to comment
Share on other sites

You could use is_int() to see if it is a number. And you could use strlen() to see if it is a certain amount of characters.

$price = $_POST['price'];

if (is_int($price) && strlen($price) == 5) {
     // good to go
}

 

What do you mean by "run a script"? strip_tags() will remove HTML tags, which could prevent XSS attacks.

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.