Posted inTechnology / Mysql / Mysqli / php / Programming / VueJs

Form Submission With Validation Using Vue.JS and PHP

Form Submission With Validation Using Vue.JS and PHP

In this Post We Will Explain About is Form Submission With Validation Using Vue.JS and PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to vuejs with php – Simple form submission Example

In this post we will show you Best way to implement Create Forms With Validation Using VueJS, hear for How to Submit Form with Validation in vuejs with PHP with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Input Form Validation in VueJS

In this Example,First of all Add or Include External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priority set.After that Include your relevant CSS Class.

Include External Libs

https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
vue.min.js
https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.10/vue.min.js	

style.css

		.div-space{
			height:30px;
		}
		.demo-label{
			margin-top:8px;
		}
		.top-margin{
			margin-top:11px;
		}
		.error{
			font-size:15px;
		}

index.php




	Pakainfo.com - Input Validation using Vue.js with PHP


	

Example of the Input Validation using Vue.js with PHP

{{ studsuccessMsg }}
Input Form
{{ studnameError }}
{{ studPassError }}
{{ studfnameError }}
{{ studlnameError }}
{{ studemailError }}
{{ studlinklError }}

liveApp.js

var liveApp = new Vue({
	el: '#validate',
	data:{
		studValidation: {studentname: '', password: '', studfname:'', studlname:'', email:'', website:''},
		studnameError: "",
		studPassError: "",
		studfnameError: "",
		studlnameError: "",
		studemailError: "",
		studlinklError: "",
		studsuccessMsg: ""
	},
 
	methods:{
		validateInput: function(){
			var valForm = liveApp.toFormData(liveApp.studValidation);
			axios.post('validate.php', valForm)
				.then(function(studRes){
					//console.log(studRes);
					if(studRes.data.studentname){
						liveApp.studnameError = studRes.data.dismsg;
						liveApp.studUnameFocus();
					}
					else if(studRes.data.password){
						liveApp.studPassError = studRes.data.dismsg;
						liveApp.studnameError = '';
						liveApp.studPassFocus();
					}
					else if(studRes.data.studfname){
						liveApp.studfnameError = studRes.data.dismsg;
						liveApp.studnameError = '';
						liveApp.studPassError = '';
						liveApp.focuFirstname();
					}
					else if(studRes.data.studlname){
						liveApp.studlnameError = studRes.data.dismsg;
						liveApp.studnameError = '';
						liveApp.studPassError = '';
						liveApp.studfnameError = '';
						liveApp.studLnameFocus();
					}
					else if(studRes.data.email){
						liveApp.studemailError = studRes.data.dismsg;
						liveApp.studnameError = '';
						liveApp.studPassError = '';
						liveApp.studfnameError = '';
						liveApp.studlnameError = '';
						liveApp.studEmailFocus();
					}
					else if(studRes.data.website){
						liveApp.studlinklError = studRes.data.dismsg;
						liveApp.studemailError = studRes.data.dismsg;
						liveApp.studnameError = '';
						liveApp.studPassError = '';
						liveApp.studfnameError = '';
						liveApp.studlnameError = '';
						liveApp.studemailError = '';
						liveApp.studWebFocus();
					}
					else{
						liveApp.studsuccessMsg = studRes.data.dismsg;
						liveApp.studnameError = '';
						liveApp.studPassError = '';
						liveApp.studfnameError = '';
						liveApp.studlnameError = '';
						liveApp.studemailError = '';
						liveApp.studlinklError = '';
					}
				});
		},
 
		studUnameFocus: function(){
			this.$refs.studentname.focus();
		},
 
		studPassFocus: function(){
			this.$refs.password.focus();
		},
 
		studFnameFocus: function(){
			this.$refs.studfname.focus();
		},
 
		studLnameFocus: function(){
			this.$refs.studlname.focus();
		},
 
		studEmailFocus: function(){
			this.$refs.email.focus();
		},
 
		studWebFocus: function(){
			this.$refs.website.focus();
		},
 
		toFormData: function(obj){
			var form_data = new FormData();
			for(var key in obj){
				form_data.append(key, obj[key]);
			}
			return form_data;
		},
 
		clearMessage: function(){
			liveApp.studsuccessMsg = '';
		}
 
	}
});

validate.php

 false, 'password' => false, 'studfname' => false, 'studlname' => false, 'email' => false, 'website' => false);
 
function formCheck_inp($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
 
$studentname=formCheck_inp($_POST['studentname']);
$password=formCheck_inp($_POST['password']);
$studfname=formCheck_inp($_POST['studfname']);
$studlname=formCheck_inp($_POST['studlname']);
$email=formCheck_inp($_POST['email']);
$website=formCheck_inp($_POST['website']);
 
if($studentname==''){
	$output['studentname']=true;
	$output['dismsg']='Student Name is required';
}
 
elseif (!preg_match("/^[a-zA-Z_1-9]*$/",$studentname)) {
	$output['studentname']=true;
  	$output['dismsg'] = "Only letters, numbers and underscore allowed"; 
}
 
elseif($password==''){
	$output['password']=true;
	$output['dismsg']='Student Password is required';
}
 
elseif($studfname==''){
	$output['studfname']=true;
	$output['dismsg']='Student Firstname is required';
}
 
elseif (!preg_match("/^[a-zA-Z ]*$/",$studfname)) {
	$output['studfname']=true;
  	$output['dismsg'] = "Only letters and white space allowed"; 
}
 
elseif($studlname==''){
	$output['studlname']=true;
	$output['dismsg']='Student Lastname is required';
}
 
elseif (!preg_match("/^[a-zA-Z ]*$/",$studlname)) {
	$output['studlname']=true;
  	$output['dismsg'] = "Only letters and white space allowed"; 
}
 
elseif($email==''){
	$output['email']=true;
	$output['dismsg']='student Email is required';
}
 
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  	$output['email']=true;
	$output['dismsg']='Invalid Student Email Format';
}
 
elseif($website==''){
	$output['website']=true;
	$output['dismsg']='Student Website is required';
}
 
elseif (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  	$output['website']=true;
	$output['dismsg']='sorry 🙂 Invalid URL';
}
 
else{
	$output['dismsg']='Vuejs Form Validated';
}

header("Content-type: application/json");
echo json_encode($output);
die();
?>

Form Validation in VueJS

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype