SlideShare a Scribd company logo
1 of 7
Download to read offline
Validation in PHP

Validation is a process by which we can prevent erroneous/incorrect data in the
form/server. To ensure valid data is collected, we apply a set of validations to data we
collect. Validation is a set of rules that you apply to the data you collect. It is very essential
to have the input to your form validated before taking the form submission data for further
processing. When there are many fields in the form, the PHP validation script becomes too
complex. Make sure you make it a habit to validate all your web pages before publishing.
When you validate web pages or a web form using a validation process, you simply check
the content of the code to ensure it is valid and free from errors.

Code

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("sunderdeep",$con);
@$a=$_POST['text_name'];
@$b=$_POST['text_mobile'];
@$c=$_POST['text_email'];
@$d=$_POST['text_user'];
@$e=$_POST['text_pass'];
@$f=$_POST['text_repass'];
if(@$_POST['insert'])
{
//echo $sql1="insert into emp values('$a','$b','$c','$d','$e','$f')";
//mysql_query($sql1);
echo "Your Data Successfully Saved";
}
?>
<html>
   <head></head>
   <script language="javascript" type="text/javascript">
   function validate()
   {
   if(document.getElementById("text_name").value=="")
   {
      alert("Please Enter Your Name");
      document.getElementById("text_name").focus();
      return false;
   }
   if(!(isNaN(document.validation.text_name.value)))
   {
      alert("Name has character only!");
      return false;
   }
   if(document.getElementById("text_mobile").value=="")
   {
      alert("Please Enter Your Mobile Number");
      document.getElementById("text_mobile").focus();



                           www.vineetsaini.wordpress.com
return false;
   }
   if((isNaN(document.validation.text_mobile.value)))
   {
       alert("Mobile has numeric only!");
       return false;
   }
   var emailPat=/^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a-
z]{2,6}(?:.[a-z]{2})?)$/i
   var emailid=document.getElementById("text_email").value;
   var matchArray = emailid.match(emailPat);
   if (matchArray == null)
   {
       alert("Your email address is wrong. Please try again.");
       document.getElementById("text_email").focus();
       return false;
   }
   if(document.getElementById("text_user").value=="")
   {
       alert("Please Enter User Name");
       document.getElementById("text_user").focus();
       return false;
   }
   if(document.getElementById("text_pass").value=="")
   {
       alert("Please Enter Your Password");
       document.getElementById("text_pass").focus();
       return false;
   }
   if(document.getElementById("text_repass").value=="")
   {
       alert("Please ReEnter Your Password");
       document.getElementById("text_repass").focus();
       return false;
   }
   if(document.getElementById("text_repass").value!="")
   {
        if(document.getElementById("text_repass").value !=
document.getElementById("text_pass").value)
        {
            alert("Confirm Password doesnot match!");
            document.getElementById("text_repass").focus();
            return false;
        }
   }
   return true;
 }
 </script>
   <body bgcolor="pink">
       <form name="validation" method="post" onsubmit="return validate();">
       <center>
          <h3><u>Validation in PHP</u></h3>
   <table border="2">


                       www.vineetsaini.wordpress.com
<tr>
     <td width="179">Name</td>
  <td><label>
    <input name="text_name" type="text" id="text_name" />
  </label></td>
 </tr>
 <tr>
  <td width="179">Phone/Mobile</td>
  <td><label>
    <input name="text_mobile" type="text" id="text_mobile" />
  </label></td>
 </tr>
 <tr>
  <td width="179">Email address </td>
  <td><label>
    <input name="text_email" type="text" id="text_email" />
  </label></td>
 </tr>
 <tr>
  <td>User Name</td>
  <td><label>
    <input name="text_user" type="text" id="text_user" />
  </label></td>
 </tr>
 <tr>
  <td>Password </td>
  <td><label>
    <input name="text_pass" type="password" id="text_pass" />
  </label></td>
 </tr>
 <tr>
  <td>Confirm Password</td>
  <td><label>
    <input name="text_repass" type="password" id="text_repass" />
  </label></td>
 </tr>
 <tr align="center">
  <td colspan="2"><label>
    <input type="submit" name="insert" value="Save" />
  </label></td>
  </tr>
</table>
</center>
</form>
</body>
</html>

Output




                      www.vineetsaini.wordpress.com
If you do not enter a name in the name column then there will be an error produced i.e.
"Please enter your name". Like as in the following image.




If you will enter a digit in the name column then there will also be an error produced i.e.
"Name has character only". Like as in the following image.




                         www.vineetsaini.wordpress.com
If you enter a character in the mobile/phone column then there will be an error produced
i.e. "Mobile has numeric only". Like as in the following image.




If you enter an incorrect format of mail id in the mail address column then there will be an
error produced i.e. "Your mail address is wrong . Please try again". Like as in the following
image.




                         www.vineetsaini.wordpress.com
If you do not enter a password in the password column then there will be an error produced
i.e. "please enter password". Like as in the following image.




If you do not re-enter a correct password then there will be an error produced i.e. "Confirm
password does not match". Like as in the following image.




                         www.vineetsaini.wordpress.com
When you fill in all columns correctly and you click on the save button then there will be an
error produced i.e. "Your data successfully saved". Like as in the following image.




Conclusion

So in this article you saw how to apply validation in a form. Using this article one can easily
understand validation in PHP.




                          www.vineetsaini.wordpress.com

More Related Content

More from Vineet Kumar Saini (20)

Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 

Recently uploaded

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Recently uploaded (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 

Validation in PHP

  • 1. Validation in PHP Validation is a process by which we can prevent erroneous/incorrect data in the form/server. To ensure valid data is collected, we apply a set of validations to data we collect. Validation is a set of rules that you apply to the data you collect. It is very essential to have the input to your form validated before taking the form submission data for further processing. When there are many fields in the form, the PHP validation script becomes too complex. Make sure you make it a habit to validate all your web pages before publishing. When you validate web pages or a web form using a validation process, you simply check the content of the code to ensure it is valid and free from errors. Code <?php $con=mysql_connect("localhost","root",""); mysql_select_db("sunderdeep",$con); @$a=$_POST['text_name']; @$b=$_POST['text_mobile']; @$c=$_POST['text_email']; @$d=$_POST['text_user']; @$e=$_POST['text_pass']; @$f=$_POST['text_repass']; if(@$_POST['insert']) { //echo $sql1="insert into emp values('$a','$b','$c','$d','$e','$f')"; //mysql_query($sql1); echo "Your Data Successfully Saved"; } ?> <html> <head></head> <script language="javascript" type="text/javascript"> function validate() { if(document.getElementById("text_name").value=="") { alert("Please Enter Your Name"); document.getElementById("text_name").focus(); return false; } if(!(isNaN(document.validation.text_name.value))) { alert("Name has character only!"); return false; } if(document.getElementById("text_mobile").value=="") { alert("Please Enter Your Mobile Number"); document.getElementById("text_mobile").focus(); www.vineetsaini.wordpress.com
  • 2. return false; } if((isNaN(document.validation.text_mobile.value))) { alert("Mobile has numeric only!"); return false; } var emailPat=/^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a- z]{2,6}(?:.[a-z]{2})?)$/i var emailid=document.getElementById("text_email").value; var matchArray = emailid.match(emailPat); if (matchArray == null) { alert("Your email address is wrong. Please try again."); document.getElementById("text_email").focus(); return false; } if(document.getElementById("text_user").value=="") { alert("Please Enter User Name"); document.getElementById("text_user").focus(); return false; } if(document.getElementById("text_pass").value=="") { alert("Please Enter Your Password"); document.getElementById("text_pass").focus(); return false; } if(document.getElementById("text_repass").value=="") { alert("Please ReEnter Your Password"); document.getElementById("text_repass").focus(); return false; } if(document.getElementById("text_repass").value!="") { if(document.getElementById("text_repass").value != document.getElementById("text_pass").value) { alert("Confirm Password doesnot match!"); document.getElementById("text_repass").focus(); return false; } } return true; } </script> <body bgcolor="pink"> <form name="validation" method="post" onsubmit="return validate();"> <center> <h3><u>Validation in PHP</u></h3> <table border="2"> www.vineetsaini.wordpress.com
  • 3. <tr> <td width="179">Name</td> <td><label> <input name="text_name" type="text" id="text_name" /> </label></td> </tr> <tr> <td width="179">Phone/Mobile</td> <td><label> <input name="text_mobile" type="text" id="text_mobile" /> </label></td> </tr> <tr> <td width="179">Email address </td> <td><label> <input name="text_email" type="text" id="text_email" /> </label></td> </tr> <tr> <td>User Name</td> <td><label> <input name="text_user" type="text" id="text_user" /> </label></td> </tr> <tr> <td>Password </td> <td><label> <input name="text_pass" type="password" id="text_pass" /> </label></td> </tr> <tr> <td>Confirm Password</td> <td><label> <input name="text_repass" type="password" id="text_repass" /> </label></td> </tr> <tr align="center"> <td colspan="2"><label> <input type="submit" name="insert" value="Save" /> </label></td> </tr> </table> </center> </form> </body> </html> Output www.vineetsaini.wordpress.com
  • 4. If you do not enter a name in the name column then there will be an error produced i.e. "Please enter your name". Like as in the following image. If you will enter a digit in the name column then there will also be an error produced i.e. "Name has character only". Like as in the following image. www.vineetsaini.wordpress.com
  • 5. If you enter a character in the mobile/phone column then there will be an error produced i.e. "Mobile has numeric only". Like as in the following image. If you enter an incorrect format of mail id in the mail address column then there will be an error produced i.e. "Your mail address is wrong . Please try again". Like as in the following image. www.vineetsaini.wordpress.com
  • 6. If you do not enter a password in the password column then there will be an error produced i.e. "please enter password". Like as in the following image. If you do not re-enter a correct password then there will be an error produced i.e. "Confirm password does not match". Like as in the following image. www.vineetsaini.wordpress.com
  • 7. When you fill in all columns correctly and you click on the save button then there will be an error produced i.e. "Your data successfully saved". Like as in the following image. Conclusion So in this article you saw how to apply validation in a form. Using this article one can easily understand validation in PHP. www.vineetsaini.wordpress.com