Jump to content

PHP validation


fdjason

Recommended Posts

For a class project no alerts are supposed to be shown at first but then after the person hits submit, warnings will show if nothing was entered into a text box.

 

Here is my code, does anybody know what I'm doing wrong?

<html>

<head>

    <title>Validate User Inputs</title>

</head>

<body>

<center>

<h2>Validate User Inputs</h2><hr>

 

<?php

    $pageValid = true;

    $postback = $_GET['postback'];

    if ($pageValid == true && $postback) {

?>

    <form>

          <?php

          $firstname= $_GET['firstName'];

          ?>

      Please enter your first name:<br>

      <input type="text" name="firstName" size="25" value="<? echo "$firstname"; ?>"><br>

          <?php

          echo "" . fValidateName($firstname) . "";

          ?>

    <br><br>

 

          <?php

          $personAge= $_GET['age'];

          ?>

      Please enter your age (13-99):<br>

      <input type="text" name="age" size="5" value="<? echo "$personAge"; ?>"><br>

          <?php

          echo "" . fValidateAge($personAge) . "";

          ?>

      <br><br>

 

 

          <?php

          $personEmail= $_GET['email'];

          ?>

      Please enter your email address:<br>

      <input type="text" name="email" size="25" value="<? echo "$personEmail"; ?>"><br>

          <?php

          echo "" . fValidateEmail($personEmail) . "";

          ?>

      <br><br>

 

          <?php

          $personState= $_GET['state'];

          ?>

      Please enter your state abbreviation:<br>

      <input type="text" name="state" size="2" value="<? echo "$personState"; ?>"><br>

          <?php

          echo "" . fValidateState($personState) . "";

          ?>

      <br><br>

 

      <input type="hidden" name="postback" value="true">

      <input type="submit" value="Submit your info!">

    </form>

 

<?php

    echo "Success!";

    } else {

//Name Validation

function fValidateName($Name) {

    if ($postback && strlen($Name) == 0) {

    echo "<font color='red'>Please enter your first name (alpha characters only)</font>";

    $pageValid = false;

    }

}

//Age Validation

function fValidateAge($age) {

    if ($postback && ($age<13||$age>99)||strlen($age) == 0) {

    echo "<font color='red'>Please enter a valid age (13-99) </font>";

    $pageValid = false;

    }

}

//Email Validation

function fValidateEmail($email) {

    $pattern = "^[\w!#$%*_\-/?|\^\{\}'~\.]+@(\w+\.)+\w{2,4}^";

    if ($postback && !(preg_match($pattern, $email))) {

    echo "<font color='red'>Please enter a valid email address ([email protected])</font>";

    $pageValid = false;

    }

}

//State Validation

function fValidateState($state){

$ValidAbbr = array("AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL",

    "IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH",

    "NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT",

    "VA","WA","WV","WI","WY");

 

    $state = strtoupper($state);

    //in_array() checks if a value exists in an array.

    if (in_array($state, $ValidAbbr)) {

    } else {echo "<font color='red'>Please enter a valid state abbreviation</font>";

    $pageValid = false;

    }

}

}

?>

 

</center>

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/199720-php-validation/
Share on other sites

This is what mine is doing: http://yorktown.cbe.wwu.edu/2010Spring/floresj/a04/Validate3.php

 

This is what it should be doing: http://yorktown.cbe.wwu.edu/sandvig/mis314/assignments/a04/Validate.php

 

I have that form there in code but something I am doing is hiding everything.  What I want to happen is to hide the red warnings (as shown in the second link) and show when the submit button is hit and one of the fields were not filled out.

Link to comment
https://forums.phpfreaks.com/topic/199720-php-validation/#findComment-1048252
Share on other sites

The things that stand out upon a cursory review are:

 

If the form hasn't been submitted, this:  if ($pageValid == true && $postback) will never evaluate as true, and will prevent the form from displaying. Also, you define $pagevalid and assign its value just prior to the conditional, so that will ALWAYS be true, defeating the purpose of the conditional.

 

You've mixed short open tags '<?' into the fields' value="<? echo $firstname ?>" whereas these will fail on servers that have short open tags disabled. Use the full <?php open tags instead.

 

This may be the biggest, yet easiest to fix: you can't use a function before it's defined. Move the functions to the head of the script, or copy them to a separate file and include() it at the beginning.

Link to comment
https://forums.phpfreaks.com/topic/199720-php-validation/#findComment-1048256
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.