Jump to content

Validation Help


mikebyrne

Recommended Posts

The validation I would require (at a glance) should be:

 

1) Only numeric can be entered on the age field

 

2) A positon from (mydropdown1) HAS to be selected and cant be left "Please Select Option"

 

3) 1 option from (mydropdown2) HAS to be selected and cant be "Please select Option"

 

4) I'll need a simple If statement saying IF the user has selected they work for a chatsite they HAVE to fill out the "If so, which one" filed

 

Again, If you can help me out it would be great

 

Here's my code


<form name="form1" method="post" action="signupsnake.php">
  <table align="center">
    <tr valign="baseline">
      <td>Name:</td>
      <td><input type="text" name="name" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td >Age:</td>
      <td><input type="number" name="age" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td>Location:</td>
      <td><input type="text" name="location" value="" size="32"></td>
    </tr>
      <tr>
      <td>Position Applying For</td>
      <td>
<select name="mydropdown1" size="1">
      <option value="Option">Please Select Option</option>
      <option value="Help Operator">Help Operator</option>
      <option value="Moderator">Moderator</option>
      <option value="Senior Moderator">Senior Moderator</option>
      <option value="IRCop">IRCop</option>
      <option value="Administrator">Administrator</option>
    </select>
      </td>
    </tr>
    <tr valign="baseline">
      <td>Reason for Application:</td>
      <td><input type="text" name="reason" value="" size="32"></td>
         </tr>
         <tr>
    <td>Do you work for any chatsite?</td>
      <td>
    <select name="mydropdown2" size="1">
     
      <option value="Option1">Please select Option</option>
      <option value="NO">NO</option>
      <option value="YES">YES</option>
         </select>
         </td>
         </tr>
      <tr valign="baseline">
      <td>If so, Which one?:</td>
      <td><input type="text" name="which" value="" size="32"></td>
    </tr>
      <tr>
      <td><input type="submit" name="submit" value="Apply"></td>
    </tr>
  </table>
</form>
</body>

 

php page

 


<?php

include('config1.php');

// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$which=$_POST['which'];

echo $name;
echo $age;
echo $location;
echo $mydropdown1;
echo $reason;
echo $mydropdown2;
echo $which;


// Insert data into database
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());


?>

 

Any help in coding the validation would be great!!!!!

Link to comment
Share on other sites

Heres The Validation stuff I use. Im sure you can mod it into what you need!

 

<?php
// Registrations
function valid_username($username)
{
  //validate username
  if(ereg("^[a-zA-Z0-9]+$",$username) && strlen($username) >= 3 && strlen($username) <= 12)
   return true;
  else
   return false;
}
function valid_password($pass1)
{
  //validate password
  if(ereg("^[a-zA-Z0-9]+$",$pass1) && strlen($pass1) >= 5 && strlen($pass1) <= 20)
  return true;
  else
  return false;
}
function valid_email($email)
{
  // validate email format
  if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email) && strlen($pass1) <= 55)
    return true;
  else
    return false;
}
function valid_name($name)
{
  // validate name format
  if(strlen($name) >= 0 && strlen($name) <= 55)
    return true;
  else
    return false;
}
function valid_location($location)
{
  // validate location format
  if(strlen($location) >= 0 && strlen($location) <= 25)
    return true;
  else
    return false;
}
function valid_msn($msn)
{
  // validate msn format
  if(strlen($msn) >= 0 && strlen($msn) <= 55)
    return true;
  else
    return false;
}
function valid_aim($aim)
{
  // validate aim format
  if(strlen($aim) >= 0 && strlen($aim) <= 55)
    return true;
  else
    return false;
}

function valid_icq($icq)
{
  // validate icq format
  if(strlen($icq) >= 0 && strlen($icq) <= 55)
    return true;
  else
    return false;
}
function valid_website($website)
{
  // validate website format
  if(strlen($website) >= 0 && strlen($website) <= 55)
    return true;
  else
    return false;
}
function valid_vatsim($vatsim)
{
  // validate vatsim format
  if(strlen($vatsim) >= 0 && strlen($vatsim) <= 10)
    return true;
  else
    return false;
}
function valid_country($country)
{
  // validate Country format
  if(ereg("^[a-zA-Z ]+$",$country) && strlen($country) >= 4 && strlen($country) <= 15)
    return true;
  else
    return false;
}
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);
}
?>

Link to comment
Share on other sites

Just a quick glance at this left me to just changed one thing

 

if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email) && strlen($pass1) <= 55)

 

should be

if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email) && strlen($email) <= 55)

 

changed the strlen to $email from $pass1.

 

Link to comment
Share on other sites

<?php

include('config1.php');

// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$which=$_POST['which'];

if ($mydropdown2 = "YES") {
     if ($which = "") {
          echo "Error, you must enter which chatsite you work for";
     }
}
else {
echo "They picked NO";
}


// Insert data into database
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());


?>

 

Have I placed the if statement in the wrong place?

 

I leave it to Yes, leave the last field blank but it still passes info to database

Link to comment
Share on other sites

I only put in echo statements so you can see if the statement works.  You need to add a error handler to it doesn't go past that and allow the data to pass.

 

I'm trying to let you do some of the work instead of all of it for you so you can learn how to do it.

Link to comment
Share on other sites

I've been playing around with it. Am I on the right track with this?

 

<?php

$errorList = array();
$name = mysql_real_escape_string ($_POST['name']);
$age = mysql_real_escape_string ($_POST['age']);
$location = mysql_real_escape_string ($_POST['location']);
$mydropdown1 = mysql_real_escape_string ($_POST['mydropdown1']);
$reason = mysql_real_escape_string ($_POST['reason']);
$mydropdown2 = mysql_real_escape_string ($_POST['mydropdown2']);



include('config1.php');


// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$which=$_POST['which'];

if ($mydropdown2 = "YES") {
     if ($which = "") {
          echo "Error, you must enter which chatsite you work for";
	  $errorList[] = 'Please enter the chatsite you worked for';
     }
}
else {
echo "They picked NO";
}


// Insert data into database
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());


?>

Link to comment
Share on other sites

Ok I'm starting to get the hang of it. Im getting an Array when I echo error so i know its finding the errorlist.

 

I'm trying to fix an if statment that only lets it insert if there is no array but the way I've tried doesn't reconise the "="

 

Could anyone point me in the right direction??

Link to comment
Share on other sites

<?php

include('config1.php');


// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$errorList = array();
$name = mysql_real_escape_string ($_POST['name']);
$age = mysql_real_escape_string ($_POST['age']);
$location = mysql_real_escape_string ($_POST['location']);
$mydropdown1 = mysql_real_escape_string ($_POST['mydropdown1']);
$reason = mysql_real_escape_string ($_POST['reason']);
$mydropdown2 = mysql_real_escape_string ($_POST['mydropdown2']);

$which=$_POST['which'];

if ($mydropdown2 == "YES") {
     if ($which == "") {
          echo "Error, you must enter which chatsite you work for";
	  $errorList[] = 'Please enter the chatsite you worked for';
     }
}
else {
echo "They picked NO";
}



if (errorlist == "Array") {
   echo "TEST";
}
else

// Insert data into database
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());


?>

 

My code still doesnt stop the data going into the database if the user selects Yes and doesnt input what site they work for.

 

The error msg seems to work fine

Link to comment
Share on other sites

I've fixed the two listbox validations

 

<?php

include('config1.php');


// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$errorList = array();
$name = mysql_real_escape_string ($_POST['name']);
$age = mysql_real_escape_string ($_POST['age']);
$location = mysql_real_escape_string ($_POST['location']);
$mydropdown1 = mysql_real_escape_string ($_POST['mydropdown1']);
$reason = mysql_real_escape_string ($_POST['reason']);
$mydropdown2 = mysql_real_escape_string ($_POST['mydropdown2']);

$which=$_POST['which'];

if ($mydropdown2 == "YES") {
     if ($which == "") {
          echo "Error, you must enter which chatsite you work for";
	  $errorList[] = 'Please enter the chatsite you worked for';
     }
}


if ($mydropdown1 == "Option") {
echo "Please select what position your applying for";
}


if (sizeof ($errorList) == 0) {
//no errors, insert into db
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());
}
else {
//errors, process errorList array
}


?>

 

What else should I do?

Link to comment
Share on other sites

Added one more

 

<?php

include('config1.php');


// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$errorList = array();
$name = mysql_real_escape_string ($_POST['name']);
$age = mysql_real_escape_string ($_POST['age']);
$location = mysql_real_escape_string ($_POST['location']);
$mydropdown1 = mysql_real_escape_string ($_POST['mydropdown1']);
$reason = mysql_real_escape_string ($_POST['reason']);
$mydropdown2 = mysql_real_escape_string ($_POST['mydropdown2']);

$which=$_POST['which'];

if ($mydropdown2 == "YES") {
     if ($which == "") {
          echo "Error, you must enter which chatsite you work for";
	  $errorList[] = 'Please enter the chatsite you worked for';
     }
}


if ($mydropdown1 == "Option") {
echo "Please select what position your applying for";
}

if ($mydropdown2 == "Option1") {
echo "Do you work for a Chatsite?";
}

if (sizeof ($errorList) == 0) {
//no errors, insert into db
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());
}
else {
//errors, process errorList array
}


?>

Link to comment
Share on other sites

<?php

include('config1.php');


// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$errorList = array();
$name = mysql_real_escape_string ($_POST['name']);
$age = mysql_real_escape_string ($_POST['age']);
$location = mysql_real_escape_string ($_POST['location']);
$mydropdown1 = mysql_real_escape_string ($_POST['mydropdown1']);
$reason = mysql_real_escape_string ($_POST['reason']);
$mydropdown2 = mysql_real_escape_string ($_POST['mydropdown2']);

$which=$_POST['which'];

if ($mydropdown2 == "YES") {
     if ($which == "") {
          echo "Error, you must enter which chatsite you work for";
	  $errorList[] = 'Please enter the chatsite you worked for';
     }
}


if ($mydropdown1 == "Option") {
echo "Please select what position your applying for";
}

if ($mydropdown2 == "Option1") {
echo "Do you work for a Chatsite?";
}

if (trim ($sname) == "") {
		$errorList[] = 'Invalid entry: Name';
		echo "please enter your name";
	}
	if (trim ($age) == "") {
		$errorList[] = 'Invalid entry: Age';
		echo "please enter your Age";
	}
	if (trim ($location) == "") {
		$errorList[] = 'Invalid entry: Location';
		echo "please enter a location";
	}
	if (trim ($location) == "") {
		$errorList[] = 'Invalid entry: Reason';
		echo "please enter a reason";
	}

if (sizeof ($errorList) == 0) {
//no errors, insert into db
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());
}
else {
//errors, process errorList array
}


?>

Link to comment
Share on other sites

I want to vaidate the the age section so the user has to enter a numeric values only.

 

What would the code be for that?

 

<?php

include('config1.php');


// table name
$tbl_name= "applications";

// values sent from form
$name=$_POST['name'];
$age=$_POST['age'];
$location=$_POST['location'];
$mydropdown1=$_POST['mydropdown1'];
$reason=$_POST['reason'];
$mydropdown2=$_POST['mydropdown2'];

$errorList = array();
$name = mysql_real_escape_string ($_POST['name']);
$age = mysql_real_escape_string ($_POST['age']);
$location = mysql_real_escape_string ($_POST['location']);
$mydropdown1 = mysql_real_escape_string ($_POST['mydropdown1']);
$reason = mysql_real_escape_string ($_POST['reason']);
$mydropdown2 = mysql_real_escape_string ($_POST['mydropdown2']);

$which=$_POST['which'];

if ($mydropdown2 == "YES") {
     if ($which == "") {
          echo "Error, you must enter which chatsite you work for";
	  $errorList[] = 'Please enter the chatsite you worked for';
     }
}


if ($mydropdown1 == "Option") {
echo "Please select what position your applying for";
}

if ($mydropdown2 == "Option1") {
echo "Do you work for a Chatsite?";
}

if (trim ($sname) == "") {
		$errorList[] = 'Invalid entry: Name';
		echo "please enter your name";
	}
	if (trim ($age) == "") {
		$errorList[] = 'Invalid entry: Age';
		echo "please enter your Age";
	}
	if (trim ($location) == "") {
		$errorList[] = 'Invalid entry: Location';
		echo "please enter a location";
	}
	if (trim ($location) == "") {
		$errorList[] = 'Invalid entry: Reason';
		echo "please enter a reason";
	}

if (sizeof ($errorList) == 0) {
//no errors, insert into db
$sql="INSERT INTO $tbl_name (Username, Age, Location, Position, Reason, Workonsite, Whatsite) VALUES ('$name', '$age', '$location', '$mydropdown1', '$reason','$mydropdown2', '$which')";
$result=mysql_query($sql)or die(mysql_error());
}
else {
//errors, process errorList array
}


?>

 

 

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.