Jump to content

Form validation


kevincro

Recommended Posts

Can someone tell me what to add or remove to the following code in order to require a user to put something in the field?  Or should I do this somewhere else?  The following is a snippet of form validation code.

 

 

function check_field2($Shipped)
{
  if(!preg_match("/[^0-9\ ]+$/",$Shipped))
    return TRUE;
  else
    return FALSE;
}

Link to comment
Share on other sites

It just has to be a number, and it can not be left blank.  Thanks for the help.

 

Well, not blank is easy enough (the code Poco provided should work), but when you say 'a number', what format are you looking for? Obviously '0' is a number, as is '000000000000000000000000000000000000'. Is there a minimum number of digits? A maximum? Are they allowed separators like '-'? Would you like to force a separator? Is there a specific format using a separator, like 3 digits, then '-', then 3 digits, another '-', then 4 digits? Or just exactly 7 digits? I think you are getting the point... you can be extremely specific or be rather flexible. The more flexible you are, the more chance someone can enter garbage (either by mistake or otherwise).

 

PhREEEk

Link to comment
Share on other sites

<?php
// For making a validation code try this:
function random_string($max = 20)
{
//create a unique confirmation code.
   $chars = explode(" ", "a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9");
     for($i = 0; $i < $max; $i++){
      $rnd = array_rand($chars);
       $rtn .= md5($chars[$rnd]);
     }
      return substr(str_shuffle(strtolower($rtn)), 0, $max);
}

// For making a thing that makes sure a username or whatever has correct cahractors and inbetween lengths try:
function valid_username($username)
{
  //validate username
  if(ereg("^[a-zA-Z0-9]+$",$username) && strlen($username) >= 3 && strlen($username) <= 12)
   return true;
  else
   return false;
}
?>

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.