UserValidation
registerNewUserController
<?php class UserValidation{ function validatefirstName($firstName){ $namePattern="/^[a-zA-Z][a-zA-Z]*$/"; if(strlen($firstName)==0){ return "First Name Should Contains Characters"; } else if(preg_match($namePattern,$firstName)!=true){ return "First name can contain only characters"; } else{ //valid firstName return ""; } }//validatefirstName function validateFullName($fullName){ $fullNamePattern="/^[a-zA-Z][a-zA-Z\\s]*$/"; if(strlen($fullName)<5){ return "Full Name Should At least 5 charcters long"; } else if(preg_match($fullNamePattern,$fullName)!=true){ return "Full name can contain only characters"; } else{ //valid fullname return ""; } }//validateFullName function validateAddress($address){ if(strlen($address)==0){ return "address is required"; } else{ return ""; } }//validateAddress function validateEmail($email){ $emailPattern="/^[a-zA-Z][a-zA-Z0-9_.]*[@][a-zA-Z0-9][a-zA-Z0-9]*[.][a-zA-Z][a-zA-Z]*$/"; if(preg_match($emailPattern,$email)!=true){ return "Invalid Email address"; } else{ //valid email return ""; } }//validateEmail function validateAge($age){ $agePattern="/^[0-9]*$/"; if(preg_match($agePattern,$age)!=true){ return "Invalid Age"; } else{ //valid age return ""; } }//validAge function validateUsername($username){ $usernamePattern="/^[a-zA-Z][a-zA-Z0-9]*$/"; if(strlen($username)==0){ return "username is required"; } else if(strlen($username)<10){ return "username should contain at least 10 characters"; } else if(strlen($username)>15){ return "username can contain only maximum 15 characters"; } else if(preg_match($usernamePattern,$username)!=true){ return "Invalid Username"; } else{ //valid age return ""; } }//validateusername function validatePasswordAndPasswordConfirmation($password,$passwordConfirmation){ if($passwordConfirmation==""){ return "white spaces are not allowed for the password"; } else if($password!=$passwordConfirmation){ return "Password does not match with the password confirmation"; } else{ return ""; } }//validatePassword }//class ?>
registerNewUserController
<?php include "../model/validation/UserValidation.php"; //registerNewUserController if(isset($_POST["isAjax"])){ //creating validation object $userValidation=new UserValidation; if(isset($_POST["username"])){ //echo "validate username"; $validationErrors=$userValidation->validateUserName(trim($_POST["username"])); if(strlen($validationErrors)!=0){ //there are validation errors echo $validationErrors; } } if(isset($_POST["firstName"])){ //echo "validate firstName"; $validationErrors=$userValidation->validatefirstName(trim($_POST["firstName"])); if(strlen($validationErrors)!=0){ //there are validation errors echo $validationErrors; } }//firstName if(isset($_POST["fullName"])){ //echo "validate fullName"; $validationErrors=$userValidation->validateFullName(trim($_POST["fullName"])); if(strlen($validationErrors)!=0){ //there are validation errors echo $validationErrors; } }//fullName if(isset($_POST["address"])){ //echo "validate address"; $validationErrors=$userValidation->validateAddress(trim($_POST["address"])); if(strlen($validationErrors)!=0){ //there are validation errors echo $validationErrors; } } if(isset($_POST["email"])){ //echo "validate email"; $validationErrors=$userValidation->validateEmail(trim($_POST["email"])); if(strlen($validationErrors)!=0){ //there are validation errors echo $validationErrors; } } if(isset($_POST["age"])){ //echo "validate age"; $validationErrors=$userValidation->validateAge(trim($_POST["age"])); if(strlen($validationErrors)!=0){ //there are validation errors echo $validationErrors; } } if(isset($_POST["passwordConf"]) && isset($_POST["password"])){ $password=trim($_POST["password"]); $passwordConf=trim($_POST["passwordConf"]); $validationErrors=$userValidation->validatePasswordAndPasswordConfirmation($password,$passwordConf); if(strlen($validationErrors)!=0){ //there are validation errors echo $validationErrors; } } }//if outer else if(isset($_POST["submit"])){ echo "Form submit request"; } else{ echo "invalid request"; } ?>
validateRegistrationFormUsingAjax
<?php include "../model/validation/UserValidation.php"; //ajax validation controller if(isset($_POST["isAjax"])){ $userValidation=new UserValidation; if(isset($_POST["username"])){ $validationErrors=$userValidation->validateUserName(trim($_POST["username"])); if(strlen($validationErrors)!=0){ echo $validationErrors; } } if(isset($_POST["firstName"])){ $validationErrors=$userValidation->validatefirstName(trim($_POST["firstName"])); if(strlen($validationErrors)!=0){ echo $validationErrors; } }//firstName if(isset($_POST["fullName"])){ $validationErrors=$userValidation->validateFullName(trim($_POST["fullName"])); if(strlen($validationErrors)!=0){ echo $validationErrors; } }//fullName if(isset($_POST["address"])){ $validationErrors=$userValidation->validateAddress(trim($_POST["address"])); if(strlen($validationErrors)!=0){ echo $validationErrors; } } if(isset($_POST["email"])){ $validationErrors=$userValidation->validateEmail(trim($_POST["email"])); if(strlen($validationErrors)!=0){ echo $validationErrors; } } if(isset($_POST["age"])){ $validationErrors=$userValidation->validateAge(trim($_POST["age"])); if(strlen($validationErrors)!=0){ echo $validationErrors; } } if(isset($_POST["passwordConf"]) && isset($_POST["password"])){ $password=trim($_POST["password"]); $passwordConf=trim($_POST["passwordConf"]); $validationErrors=$userValidation->validatePasswordAndPasswordConfirmation($password,$passwordConf); if(strlen($validationErrors)!=0){ echo $validationErrors; } } }//if outer //ajax request is to validate all data before submiting the form if(isset($_POST["isAllData"])){ $userValidation=new UserValidation; //$userValidationError="<b> The following Error Appear in Your Form </b><br/>"; $userValidationError=""; if(isset($_POST["username"])){ $validationErrors=$userValidation->validateUserName(trim($_POST["username"])); if(strlen($validationErrors)!=0){ $userValidationError=$userValidationError."".$validationErrors."<br/>"; } } if(isset($_POST["firstName"])){ $validationErrors=$userValidation->validatefirstName(trim($_POST["firstName"])); if(strlen($validationErrors)!=0){ $userValidationError=$userValidationError."".$validationErrors."<br/>"; } }//firstName if(isset($_POST["fullName"])){ $validationErrors=$userValidation->validateFullName(trim($_POST["fullName"])); if(strlen($validationErrors)!=0){ $userValidationError=$userValidationError."".$validationErrors."<br/>"; } }//fullName if(isset($_POST["address"])){ $validationErrors=$userValidation->validateAddress(trim($_POST["address"])); if(strlen($validationErrors)!=0){ $userValidationError=$userValidationError."".$validationErrors."<br/>"; } } if(isset($_POST["email"])){ $validationErrors=$userValidation->validateEmail(trim($_POST["email"])); if(strlen($validationErrors)!=0){ $userValidationError=$userValidationError."".$validationErrors."<br/>"; } } if(isset($_POST["passwordConf"]) && isset($_POST["password"])){ $password=trim($_POST["password"]); $passwordConf=trim($_POST["passwordConf"]); $validationErrors=$userValidation->validatePasswordAndPasswordConfirmation($password,$passwordConf); if(strlen($validationErrors)!=0){ $userValidationError=$userValidationError."".$validationErrors."<br/>"; } } if(isset($_POST["age"])){ $validationErrors=$userValidation->validateAge(trim($_POST["age"])); if(strlen($validationErrors)!=0){ $userValidationError=$userValidationError."".$validationErrors."<br/>"; } } if(strlen($userValidationError)!=0){ //there are validation errors echo $userValidationError; } else{ //there no validation errors echo "valid"; } } ?>
view
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript"> function createXmlHttpObject(){ var xmlhttp=""; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } function sendAjaxHttpRequest(xmlhttp,url,parameterName,parameterValue){ var parameters=parameterName+"="+parameterValue+"&isAjax=true"; xmlhttp.open("POST",url,true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", parameters.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(parameters); } function validateFormItem(elementID,elementValue,spanTagId){ if (elementValue.length==0) { document.getElementById(spanTagId).innerHTML=""; return; } xmlhttp=createXmlHttpObject(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtAllErrors").innerHTML=""; document.getElementById(spanTagId).innerHTML=xmlhttp.responseText; } } sendAjaxHttpRequest(xmlhttp,"../controller/validateRegistrationFormController.php",elementID,elementValue); } //password validation function passwordValidation(passwordConf){ var password=(document.getElementById("password")).value; if (passwordConf.length==0) { document.getElementById("txtPasswordConf").innerHTML=""; return; } xmlhttp=createXmlHttpObject(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtPasswordConf").innerHTML=""; document.getElementById("txtPasswordConf").innerHTML=xmlhttp.responseText; } } var parameters="password="+password+"&passwordConf="+passwordConf+"&isAjax=true"; xmlhttp.open("POST","../controller/validateRegistrationFormController.php",true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", parameters.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(parameters); } </script> </head> <body> <h2>User Registration form </h2> <form method="POST" action="../controller/registerNewUserController.php" id="userRegistrationForm" name="userRegistrationForm"> UserName <input type="text" name="username" id="username" onChange="validateFormItem('username',this.value,'txtUsername')"/><p><span id="txtUsername"></span></p> FirstName <input type="text" name="firstName" id="firstName" onChange="validateFormItem('firstName',this.value,'txtFirstName')"/><p><span id="txtFirstName"></span></p> fullName <input type="text" name="fullName" id="fullName" onChange="validateFormItem('fullName',this.value,'txtFullName')"/><p><span id="txtFullName"></span></p> Age <input type="text" name="age" id="age" onChange="validateFormItem('age',this.value,'txtAge')"/><p><span id="txtAge"></span></p> Address <input type="text" name="address" id="address" onChange="validateFormItem('address',this.value,'txtAddress')"/><p><span id="txtAddress"></span></p> Email <input type="text" name="email" id="email" onChange="validateFormItem('email',this.value,'txtEmail')"/><p><span id="txtEmail"></span></p> password <input type="password" name="password" id="password" /><p><span id="txtPassword"></span></p> password confirmation <input type="password" name="passwordConf" id="passwordConf" onChange="passwordValidation(this.value)"/><p><span id="txtPasswordConf"> </span></p> <input id="submit" type="submit" name="submit" value="Register" /> </form> </body> </html>
No comments:
Post a Comment