文本框旁边的表单验证错误消息

时间:2014-01-04 18:57:33

标签: php forms validation

我在PHP中创建了一个表单,并希望验证文本框本身旁边的表单条目。我尝试了Display form validation errors next to each field中发布的方法,但它给了我一个错误。这是我的代码:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<table border="0" cellpadding="0" cellspacing="0" width="95%" align="right">
<tr>
<td>&nbsp;</td>
</tr>
<tr valign="top">
<td><label>First Name:</label> <input type="text" size="25" maxlength="25" 
name="first_name" id="first_name" /></td>   
</tr>

<?php
if(isset($_POST['submit'])){
$first_name = $_POST['first_name']; 
$last_name = $_POST['last_name'];

if (empty($first_name) && empty($last_name) && empty($email_address) && 
empty($user_age) && empty($gender) && empty($occupation) && empty($company_name) && 
empty($user_name) && empty($password)) {
?>
<td><?php  echo "Please Enter All Values"; ?></td>
<?php
}
elseif ((empty($first_name)) || (strlen($first_name)>25)) {
?>
<td><?php echo "Enter valid First Name that is less than 25 characters"; ?></td>
<?php
}

1 个答案:

答案 0 :(得分:0)

检查一下。可能有用。 http://css-tricks.com/almanac/selectors/r/required/

如果您使用的是HTML5,请尝试使用此功能。 http://www.the-art-of-web.com/html/html5-form-validation/