Numbers only in a form

A

Anonymous

Guest
Hi,
I have a text field, and I need help to make something that will check character by character if its 1-2-3-4-5-6-7-8-9-0, no * ( ) - = + or letters.

I know the function preg_match and is_numeric but I think i don't use them correctly because it's not working.

if (preg_match("/[0-9]/", $asdf)) {
mysql_query($result5);
}else{
echo"<center><br><br><b> invalid. </center></b>";
exit();
}
}

and when I load the page it always return invalid. I dont even have the time to write something in the text field.

Anyone got an idea?
thanks,
--eh
 
If all that you are looking for is whether the input has anything but numbers then you should try
Code:
ereg( string pattern, string string [, array regs])
and eregi() finctions. They search the string for regular expressions given in pattern, its case sensetive for ereg and non case sensetive way for eregi. So i think from yourquestion the code will look like this:
Code:
if (ereg('[^a-zA-Z\*\(\)\-\=\+]$', $asdf))
{
echo"<center><br><br><b> invalid. </center></b>";
}
else
{
mysql_query($result5);
}
or another way if you just want to check if the variable is just numeric and does not contain any other possible characters
Code:
if (ereg('^[0-9]$', $asdf))
return true;
else
return false;

fore more examples go Here for more information and examples.
 
I am also trying to take a credit card number from a htm post variable and make sure that the number has nothing but numbers.

I tried using both of those examples and its not working. Here is what I have.

//This I get from the prior page
$card_num= $HTTP_POST_VARS['card_num'];

if (ereg('^[0-9]$', $card_num))
{
display_error("GOOD");
}
else
{
display_error("BAD");
}

//or
if (ereg('[^a-zA-Z\*\(\)\-\=\+]$',$card_num))

{
display_error("GOOD");
}
else
{
display_error("BAD");
}

The first example always returns bad regardless of what the string is. The second one will say bad for example: 213343432343JJ (Where the 2 J's are the problem) but will say good for example: jadsh7897987987

Any suggestions??
 
hey sorry i couldnt reply for a while... my internet got diconnected... stupid providers.

anyways try this

Code:
if (ereg('^[a-zA-Z\*\(\)\-\=\+]$',$card_num)) 

{ 
display_error("GOOD"); 
} 
else 
{ 
display_error("BAD"); 
}
The problem with this one is solely in the synthax of that ereg string

And the first one i think you have to flip over like this:
Code:
if (ereg('^[0-9]$', $card_num)) 
{ 
display_error("BAD"); 
} 
else 
{ 
display_error("GOOD"); 
}
Sorry wasnt thinking, but i was in school so i didnt have time to doublecheck everything


they both should work now...

P.S. You know i gave you a link last time to a page that has all the information that you need to use the function with some examples.
 
Ok actually it works at 50%.

I tryed both ereg('^[0-9]$' and preg_match("/^[0-9]*$/"

and I have the same problem with both method.

When the client put * - etc in the form, and he press submit, it returns false and say ' blah blah invalid etc..' BUT when the client hit refresh, it post things again and then it works.

So they put 10*9999 and it dont work then they hit refresh and it return true.

I dunno if you can understand what I'm trying to explain.

:help: :ph34r: :help:

Thanks for the help...

--eh
 
Hey you can try to assign a null value to a variable when the ereg function returns false.

I dont know if you understand what i am saying but here's an example

Code:
<?
$num=$HTTP_POST_VARS['num'];
if ($num=='')
die('You did not fill out all the required fields");
if (ereg('^[0-9]$', $card_num)) 
 { 
  foreach(get_defined_vars() as $key)
  {
   unset($key);
  }
  display_error("BAD"); 
 } 
else 
 { 
  display_error("GOOD"); 
 } 
}

P.S. if you have any questions about the functions used go to http://www.php.net
 
why don't you str_replace, a * with "" or any empty string, I think that would solve the problem!?
 
Back
Top