Today, We will explain to you how to make form validation in PHP(Php Form Validation Example). when the user submitted the form at that time required for input field validation. there are two types of validation, the first is server-side and the second is client-side. see below example of server-side validation(Php Form Validation Example).
Required field, email validation and number required .. etc I will give validation information by our post.
See below code, that is an array wise creating a form validation, whenever error generate at that time count error array element, if count greater than 0, we are showing the error message.
<?php if(isset($_POST['btnadd'])) { $first_name = $_POST['txtFname']; $last_name = $_POST['txtLname']; $address = $_POST['txtAddress']; $email = $_POST['txtEmail']; $mobile = $_POST['txtMobile']; $error=array(); if(empty($first_name)){ $error['first_name']="First Name is required"; } if(empty($last_name)){ $error['last_name']= "Last Name is required"; } if(empty($email)){ $error['email']= "Email is required"; } else { if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $error['email']= "Invalid email format"; } } if (empty($mobile)) { $error['mobile']= "Mobile is required"; } else{ if (preg_match('/[^0-9]+$/', $mobile)){ $error['mobile']= "Only Number allowed"; } } $count=count($error); if($count>0){ echo "Validation is Success"; } } ?> <style> .error {color: #FF0000;} </style> <form method="post" name="frmAdd"> <table align="center"> <tr> <td colspan="2" align="center">Add Record</td> </tr> <tr> <td>First Name</td> <td> <input type="text" name="txtFname" value="<?php echo @$first_name; ?>"> <span class = "error">* <?php echo '<br>'.@$error['first_name'];?></span> </td> </tr> <tr> <td>Last Name</td> <td> <input type="text" name="txtLname" value="<?php echo @$last_name; ?>"> <span class = "error">* <?php echo '<br>'.@$error['last_name'];?></span> </td> </tr> <tr> <td>Address</td> <td><textarea name="txtAddress" rows="4" cols="16"></textarea> </td> </tr> <tr> <td>Email</td> <td> <input type="text" name="txtEmail" value="<?php echo @$email; ?>"> <span class = "error">* <?php echo '<br>'.@$error['email'];?></span> </td> </tr> <tr> <td>Mobile</td> <td> <input type="text" name="txtMobile" value="<?php echo @$mobile; ?>"> <span class = "error">* <?php echo '<br>'.@$error['mobile'];?></span> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="Add" name="btnadd"> </td> </tr> </table> </form>
Please follow and like us:
Hi there, I read your blogs on a regular basis.
Your writing style is awesome, keep doing what you’re doing!
Thanks!
I’ve learn a few just right stuff here. Certainly price bookmarking for revisiting.
I wonder how a lot attempt you set to create this sort of fantastic informative site.
Thank you for the good writeup. It in fact was a amusement account
it. Look advanced to far added agreeable from you!
However, how can we communicate?
thanks
I read this piece of writing completely regarding
the difference of most up-to-date and preceding technologies, it’s remarkable article.
Wow, this article is fastidious, my younger sister is analyzing these kinds of
things, therefore I am going to convey her.
Your mode of describing the whole thing in this article is truly nice, all be
capable of easily understand it, Thanks a lot.
Magnificent goods from you, man. I’ve understand your stuff previous to and you are just extremely magnificent.
I really like what you’ve acquired here, certainly like what you’re
saying and the way in which you say it. You make it enjoyable and you still take care of to keep it smart.
I cant wait to read much more from you. This is really a tremendous site.
Keep this going please, great job!
Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is great, as well as the content!
I’m truly enjoying the design and layout
of your site. It’s a very easy on the eyes which makes it much
more enjoyable for me to come here and visit more often. Did you hire out a developer to create your theme?
Great work!
Hello! Do you use Twitter? I’d like to follow you if that would be okay.
I’m absolutely enjoying your blog and look forward to
new posts.
I think the admin of this website is actually working hard
in support of his site, for the reason that here every stuff is quality based material.
Thank you for some other informative website.
Where else could I get that type of info written in such a perfect approach?
I’ve a challenge that I’m simply now working on, and I’ve been at the look out for such information.
Very energetic blog, I loved that bit. Will there be a part 2?
Hi there, I enjoy reading all of your post.
I wanted to write a little comment to support you.
Hi, I do think this is an excellent web site. I stumbledupon it 😉 I may come back once again since
i have saved as a favorite it. Money and freedom is the greatest way
to change, may you be rich and continue to help others.
Hello there! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community in the same niche.
Your blog provided us useful information to work
on. You have done a marvellous job!
Your style is really unique in comparison to other people I’ve
read stuff from. I appreciate you for posting when you have the opportunity, Guess I’ll just bookmark this blog.
This website was… how do I say it? Relevant!!
Finally I have found something which helped me. Appreciate it!
Hi there I am so happy I found your blog page, I really found you by error,
while I was researching on Bing for something else, Anyhow I am here
now and would just like to say kudos for a remarkable post and a all round
exciting blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have saved it and also added your RSS feeds, so when I
have time I will be back to read more, Please do keep up the superb work.
Thanks so much for the blog article.Much thanks again.
I am really grateful to the holder of this web site
who has shared this impressive article at here.
Thanks-a-mundo for the post.Really looking forward to read more. Great.
Thank you for the auspicious writeup. It in fact used to be a amusement account it. Glance complicated to more brought agreeable from you! By the way, how can we communicate?
Simply wish to say your article is as amazing. The clearness in your post is simply
excellent and i could assume you’re an expert on this subject.
Well with your permission allow me to grab your feed to keep updated with forthcoming
post. Thanks a million and please keep up the rewarding work.
Also visit my webpage; LeviFRuman