Jump to content
Killersites Forums

HTML fields check


Recommended Posts

suppose I have 3 form fields Firstname, lastname and username and I want to check these fields using PHP. whenever I checked field is empty or does not meet any requirement then form displays error against to every field. please guide me exact program that error only display to those field which does not meet requirement.

please go through my code:-

<?php

$firstname=$_POST['Fname'];

$lastname=$_POST['Lname'];

$Username=$_POST['Username'];

$err="";

if(isset($submit)){

if($firstname==""){

$err = "<i style='color:red'>Please enter your first name!</i>";

}else{

if(!$firstname==preg_match("/^[a-zA-Z]{3,20}$/",$firstname)){

$err = "<i style='color:red'>please enter your correct name!</i>";

}else{

$err = "";

if($lastname==""){

$err = "<i style='color:red'>Please enter you last name!</i>";

}else{

if(!$lastname==preg_match("/^[a-zA-Z]{3,20}$/",$lastname)){

$err = "<i style='color:red'>please enter your correct name!</i>";

}else{

if($Username==""){

$err = "<i style='color:red'>please enter your new username!</i>";

}else{

if(!$Username==preg_match("/^[a-zA-Z0-9].*{?=.*\w}.*{5,20}$/",$Username)){

$err = "<i style='color:red'>incorrect username!</i>";

}

}

}

}

}

}

?>

<table border="1" cellpadding="0" cellspacing="0">

<tr><td align="right">First name:</td>

<td><form name="test" action="samefile.php" method="post"><input type="text" name="Fname"></td><td><?php echo $err; ?></td>

</tr>

<tr>

<td align="right">Last name:</td>

<td><input type="text" name="Lname"></td>

<td><?php echo $err; ?></td>

</tr>

<tr>

<td align="right">Username:</td>

<td><input type="text" name="Username"></td>

<td><?php echo $err; ?></td>

</tr></form></table>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...