/    Sign up×
Community /Pin to ProfileBookmark

form validation

Hi

the below code if a field on a form. I have been displaying a message at the top of the page to display validation errors, however I would like to change it so that the text next to the field will turn red. any idea how to fix the below code to do this

thank you

[code=php]
<tr>
<td style=”width:25%;” class=”tableRowBottomBorder”><span style=”width:25%;”><label for = “name” <?php if(!$given_to){ echo ‘class=”warning”‘; } ?>>Given To:</label></span></td>
<td style=”width:75%;” class=”tableRowBottomBorder tableLeftBorder” ><label>
<input type=”text” name=”givenTo” id=”givenTo” />
</label></td>
</tr>
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@kprocauthorFeb 04.2008 — I found this code but seems a little complex.

my goal is to change the color of the text for the form label

[code=php]
<?
function error_bool($error, $field) {
if($error[$field]) {
print("<td style=color:red>");
}
else {
print("<td>");
}
}

function show_form() {
global $HTTP_POST_VARS, $print_again, $error;
?>

<form action="" method="post">
<table width="400" border="1" cellspacing="0" cellpadding="0">
<tr>
<?php error_bool($error, "name"); ?> Name </td>
<td><input name="name" type="text" id="name" value="<? echo $_POST["name"]; ?>"></td>
</tr>
<tr>
<?php error_bool($error, "email"); ?> Email </td>
<td><input name="email" type="text" id="email" value="<? echo $_POST["email"]; ?>"></td>
</tr>

<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</form>
<?
}
if(isset($_POST["Submit"])) {
check_form();
} else {
show_form();
}

function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_{|}~-][A-Za-z0-9!#$%&amp;'*+/=?^_{|}~.-]{0,63})|("[^(\|")]{0,62}"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^[?[0-9.]+]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}


function check_form()
{
global $HTTP_POST_VARS, $error, $print_again;
$error['name'] = false;
if($_POST["name"]=="") {
$error['name'] = true;
$print_again = true;
$message="The name field is empty<br>";
}
if(!check_email_address($_POST['email'])) {
$error['email'] = true;
$print_again = true;
$message.="Either Field Empty or Invalid Email ID <br>";
}
if($print_again) {
show_form();

} else {
show_form();
$message="All Fields are valid <br>
Now, In this way you can validate the other textfield as well<br>
You can insert data into table";
}
echo "$message";
}

?>
[/code]
Copy linkTweet thisAlerts:
@kprocauthorFeb 04.2008 — Hi

The below is working and doing what I want, however I'm going to add script to add information to a mysql table. How do i get the script to stop if there was an error and not add to the table

thanks

<i>
</i>function error_bool($error, $field) {
if($error[$field]) {
echo '&lt;td style="width:25%; color:red;" class="tableRowBottomBorder"&gt;';

<i> </i> }
<i> </i> else {
<i> </i> echo '&lt;td style="width:25%;" class="tableRowBottomBorder"&gt;';
<i> </i> }
<i> </i>}

function show_form() {
global $HTTP_POST_VARS, $print_again, $error;
?&gt;
&lt;?
}
if(isset($_POST["updateGift"])) {
check_form();
} else {
show_form();
}


function check_form()
{
global $HTTP_POST_VARS, $error, $print_again;

$error['givenBy'] = false;
if($_POST["givenBy"]=="") {
$error['givenBy'] = true;
$print_again = true;

<i> </i>}
$error['givenTo'] = false;
if($_POST["givenTo"]=="") {
$error['givenTo'] = true;
$print_again = true;

<i> </i>}

<i> </i>$error['receiveDate'] = false;
<i> </i>if($_POST["receiveDate"]=="") {
<i> </i> $error['receiveDate'] = true;
<i> </i> $print_again = true;

<i> </i>}
×

Success!

Help @kproc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...