Tagged: vue server side validation

Form Submission With Validation Using Vue.JS and PHP 0

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

<!DOCTYPE html>
<html>
<head>
	<title>Pakainfo.com - Input Validation using Vue.js with PHP</title>
</head>
<body>
	<div class="container">
		<h1 class="page-header text-center">Example of the Input Validation using Vue.js with PHP</h1>
		<div id="validate">
			<div class="row">
				<div class="col-xl-10 col-xl-offset-1">
					<div class="alert alert-success text-center" v-if="studsuccessMsg">
						<button type="button" class="close" @click="clearMessage();"><span aria-hidden="true">×</span></button>
						<span class="glyphicon glyphicon-check"></span> {{ studsuccessMsg }}
					</div>
					<div class="panel panel-default">
						<div class="panel-heading">
							Input Form
						</div>
						<div class="panel-body">
							<div class="top-margin"></div>
							<div class="row">
								<div class="col-xl-6">
									<div class="live form-group">
										<div class="col-xl-3">
											<label class="demo-label">Student Name:</label>
										</div>
										<div class="col-xl-9">
											<input type="text" ref="studentname" class="form-control" v-model="studValidation.studentname">
											<div v-if="studnameError" class="error">{{ studnameError }}</div>
										</div>
									</div>
								</div>
								<div class="col-xl-6">
									<div class="live form-group">
										<div class="col-xl-3">
											<label class="demo-label">Password:</label>
										</div>
										<div class="col-xl-9">
											<input type="password" ref="password" class="form-control" v-model="studValidation.password">
											<div v-if="studPassError" class="error">{{ studPassError }}</div>
										</div>
									</div>
								</div>
							</div>
							<div class="div-space"></div>
							<div class="row">
								<div class="col-xl-6">
									<div class="live form-group">
										<div class="col-xl-3">
											<label class="demo-label">Firstname:</label>
										</div>
										<div class="col-xl-9">
											<input type="text" ref="studfname" class="form-control" v-model="studValidation.studfname">
											<div v-if="studfnameError" class="error">{{ studfnameError }}</div>
										</div>
									</div>
								</div>
								<div class="col-xl-6">
									<div class="live form-group">
										<div class="col-xl-3">
											<label class="demo-label">Lastname:</label>
										</div>
										<div class="col-xl-9">
											<input type="text" ref="studlname" class="form-control" v-model="studValidation.studlname">
											<div v-if="studlnameError" class="error">{{ studlnameError }}</div>
										</div>
									</div>
								</div>
							</div>
							<div class="div-space"></div>
							<div class="row">
								<div class="col-xl-6">
									<div class="live form-group">
										<div class="col-xl-3">
											<label class="demo-label">Email:</label>
										</div>
										<div class="col-xl-9">
											<input type="text" ref="email" class="form-control" v-model="studValidation.email">
											<div v-if="studemailError" class="error">{{ studemailError }}</div>
										</div>
									</div>
								</div>
								<div class="col-xl-6">
									<div class="live form-group">
										<div class="col-xl-3">
											<label class="demo-label">Website:</label>
										</div>
										<div class="col-xl-9">
											<input type="text" ref="website" class="form-control" v-model="studValidation.website">
											<div v-if="studlinklError" class="error">{{ studlinklError }}</div>
										</div>
									</div>
								</div>
							</div>
							<div class="div-space"></div>
						</div>
						<div class="panel-footer">
							<button class="btn btn-default" @click="validateInput();"><span class="glyphicon glyphicon-check"></span> Validate</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
<script src="liveApp.js"></script>
</body>
</html>

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

<?php
 
$output = array('studentname' => 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.