Jump to content

How to check if a variable has a number on it?


bugzy

Recommended Posts

Newbie question guys..

 

I want to check if a user input/include a number on the textbox?

 

 

so the requirement is, user must input a string and a number on the textbox and no other conditions just want to check if he/she input a number on it.

Link to comment
Share on other sites

Question is too broad.

 

Pikachu2000 I already edited it already.

 

to make it clear

 

if a user input - "hey you are beautiful"

 

it should return false.

 

if a user input - "hey you are beautiful 2222"

 

it should return true.

Link to comment
Share on other sites

Probably overkill but yeah...

 

function containsInteger($input) {
return (boolean)preg_match("/[0-9]/", $input);
}

 

 

Hello,

 

I tried this

 

 

preg_match("/[0-9]/", $address) == FALSE

 

and this

 

!preg_match("/[0-9]/", $address)

 

 

But it's not working like...

 

like if I input "Hello World"

 

It's returning a true value even if there's no number included on the input

 

:shrug:

 

Link to comment
Share on other sites

 

if(strlen($address) < 20 && !empty($address) && !preg_match("/[0-9]/", $address))
   {
      //invalid
   }

 

As Pikachu stated that could be written more logically and it actually has a flaw. If the user entered the number '0' the validation would fail because the function empty() would return false. But, let's step back a second because that is really overkill. If you must have a check to ensure there is at least 1 number in the value there is no need to do the strlen() or empty() checks at all!

 

Also, I normally do an isset() check on form field values. It could be considered overkill, but I incorporate it with a process to also trim the values which you should be doing anyway. So, here's my take:

 

$address = isset($_POST['address']) ? trim($_POST['address']) : '';
if(!preg_match("/[0-9]/", $address ))
{
    //Invalid
    echo "The value '$address' is invalid"; //Add this for debugging
}

Link to comment
Share on other sites

 

if(strlen($address) < 20 && !empty($address) && !preg_match("/[0-9]/", $address))
   {
      //invalid
   }

 

As Pikachu stated that could be written more logically and it actually has a flaw. If the user entered the number '0' the validation would fail because the function empty() would return false. But, let's step back a second because that is really overkill. If you must have a check to ensure there is at least 1 number in the value there is no need to do the strlen() or empty() checks at all!

 

Also, I normally do an isset() check on form field values. It could be considered overkill, but I incorporate it with a process to also trim the values which you should be doing anyway. So, here's my take:

 

$address = isset($_POST['address']) ? trim($_POST['address']) : '';
if(!preg_match("/[0-9]/", $address ))
{
    //Invalid
    echo "The value '$address' is invalid"; //Add this for debugging
}

 

Psycho thank you very much for the reminder.. It's appreciated. But what I posted is just the summary of the code.. There's actually more into that like trimming, mysql escape and etc.

 

Anyway, Thanks again and it will be for sure noted for future work  8)

 

 

 

 

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.