Jump to content

Help with function


Trium918

Recommended Posts

Problem: write a script that will only allow the

user to enter ,a-z A-Z 0-9, letters and numbers.

I have another script that I am using to validate

an email address and it's working using the same method.

Unfortunately, this one isn't working.

 

Note: Just Ideas

 

<?php
//clean out any SQL injections, but limited to numbers and letters only  
   if (!valid_entry($_POST['user_name']))
   {
      do_html_header("Problem:");
      echo "<p class=\"genmed\">That is not valid a user name.  Please go back "
           ." and try again.</p>";
      do_html_footer();
      exit; 
   }
?>

 

<?php
function valid_entry($user_name)
{
  if(ereg("/[^a-zA-Z0-9]/",$user_name))
  	return true;
  else
             return false;			
}
?>

Link to comment
Share on other sites

why not...

 

<?php
function valid_entry($user_name)
{
  return ereg("^[a-zA-Z0-9]+$",$user_name);	
}
?>

 

but yes, you can do it like that, but most people use brackets to keep it easy to read

Thanks

 

Is it possible to set an array for every field that will

be using numbers and letters and place that variable

where $user_name is and would I have to have a loop?

Link to comment
Share on other sites

I donnot want to have to create this for every

form field.

 

<?php
// clean out any SQL injections, but limited to numbers and letters only  
   if (!valid_entry($_POST['address']))
   {
   	  do_html_header("Problem:");
      echo "<p class=\"genmed\">That is not valid a user name.  Please go back "
           ." and try again.</p>";
      do_html_footer();
      exit; 
   }

// clean out any SQL injections, but limited to numbers and letters only  
   if (!valid_entry($_POST['lastname']))
   {
   	  do_html_header("Problem:");
      echo "<p class=\"genmed\">That is not valid a user name.  Please go back "
           ." and try again.</p>";
      do_html_footer();
      exit; 
   }

// clean out any SQL injections, but limited to numbers and letters only  
   if (!valid_entry($_POST['firstname']))
   {
   	  do_html_header("Problem:");
      echo "<p class=\"genmed\">That is not valid a user name.  Please go back "
           ." and try again.</p>";
      do_html_footer();
      exit; 
   }




?>

 

<?php
function valid_entry($user_name)
{
  if(ereg("^[a-zA-Z0-9]+$",$user_name))
  	return true;
  else
  	return false;			
}
?>

Link to comment
Share on other sites

you could

 

<?php
foreach($_POST as $P)
{
   if (!valid_entry($P))
   {
   	  do_html_header("Problem:");
      echo "<p class=\"genmed\">That is not valid data.  Please go back "
           ." and try again.</p>";
      do_html_footer();
      exit; 
   }
}
?>

 

the address would fail

Link to comment
Share on other sites

you could

 

<?php
foreach($_POST as $P)
{
   if (!valid_entry($P))
   {
   	  do_html_header("Problem:");
      echo "<p class=\"genmed\">That is not valid data.  Please go back "
           ." and try again.</p>";
      do_html_footer();
      exit; 
   }
}
?>

 

the address would fail

 

I am getting the echo

Link to comment
Share on other sites

<?php
function valid_entry($entries)
{
   $valid = true;
   foreach((array)$entries as $entry)
   {
      if(!ereg("^[a-zA-Z0-9]+$", $entry))
      {
         $valid = false;
      }
   }
   return $valid;
}

$entries = array($_POST['username'], $_POST['password']);
if(!valid_entry($entries))
{
   echo 'Bad entry!';
}
?>

 

you could pass that function a string or an array.

Link to comment
Share on other sites

<?php
function valid_entry($entries)
{
   $valid = true;
   foreach((array)$entries as $entry)
   {
      if(!ereg("^[a-zA-Z0-9]+$", $entry))
      {
         $valid = false;
      }
   }
   return $valid;
}

$entries = array($_POST['username'], $_POST['password']);
if(!valid_entry($entries))
{
   echo 'Bad entry!';
}
?>

 

you could pass that function a string or an array.

 

It is giving me the echo 'bad entry'; even when

there is only letters and numbers.

Link to comment
Share on other sites

Sorry to be a snake in the grass, but been there got a t-shirt. What is a valid email address.

@aol.com @biz.co.uk endless ?

my atempt was it had to have one and only one @

only one dot after

and have 1 or more charecters before the @

emails I think have to start with a letter a - z

True email validation is very hard

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.