Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ajax/PHP form validation [closed]
- <script>
- $.validator.setDefaults({
- submitHandler: function() { alert("submitted!"); }
- });
- $(document).ready(function(){
- $("#signupForm").validate({
- rules: {
- firstname:{
- required: true,
- minlength: 2
- },
- lastname: "required",
- username: {
- required: true,
- minlength: 2
- },
- password: {
- required: true,
- minlength: 5
- },
- confirm_password: {
- required: true,
- minlength: 5,
- equalTo: "#password"
- },
- email: {
- required: true,
- email: true
- },
- topic: {
- required: "#newsletter:checked",
- minlength: 2
- },
- agree: "required"
- },
- messages: {
- firstname:{
- required:"Please enter your firstname",
- minlength: "Your firstname must be more than 2 characters"
- },
- lastname: "Please enter your lastname",
- username: {
- required: "Please enter a username",
- minlength: "Your username must consist of at least 2 characters"
- },
- password: {
- required: "Please provide a password",
- minlength: "Your password must be at least 5 characters long"
- },
- confirm_password: {
- required: "Please provide a password",
- minlength: "Your password must be at least 5 characters long",
- equalTo: "Please enter the same password as above"
- },
- email: "Please enter a valid email address",
- agree: "Please accept our policy"
- }
- })
- });
- (function($) {
- $(document).ready(function() {
- $('#signupForm').ajaxSubmit(){
- $Ajax({
- type: 'POST',
- url: 'regprocess.php',
- dataType: 'json',
- success: function(json, textStatus){
- $('#signupForm label .error').append(json);
- },
- error : function(xhr, textStatus, errorThrown){
- alert: ('An error occurred! ' + ( errorThrown ? errorThrown : xhr.status )
- });
- };
- });
- })
- (jQuery);
- </script>
- And PHP......
- <?php
- //form processing
- //import form info
- $firstname = $_POST['firstname'];
- $lastname = $_POST['lastname'];
- $username = $_POST['username'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- $confirm_password = $_POST['confirm_password'];
- $myArray[] = $errors;
- if(isset($_POST['submit'])) {
- function validate($data){
- //validate here, return array of errors
- if(!isset($data)){
- print 'Please enter your ' + $data;
- }
- else if(strlen($data) <=2){
- print 'Your ' + $data + 'must be more than 2 characters';
- }else{//data is fine send to encryption
- $data = true;
- return $data;
- };
- validate($firstname);
- validate($lastname);
- validate($username);
- function is_valid_email_address($email){
- $qtext = '[^\x0d\x22\xSc\x80-\xff]';
- $dtext = '[^\x0d\x5b-\x5d\x80-\xff]';
- $atom = '[^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c' .
- '\x3e\x40\x5b-\x5d\x7f-\xff]+';
- $quoted_pair = '\x5c[\x00-\x7f]';
- $domain_literal = "\x5b($dtext|$quoted_pair)*\x5d";
- $quoted_string = "\x22($qtext|$quoted_pair)*\x22";
- $domain_ref = $atom;
- $sub_domain = "($domain_ref|$domain_literal)";
- $word = "($atom|$quoted_string)";
- $domain = "$sub_domain(\x2e$word)*";
- $local_part = "$word(\x2e$word)*";
- $addr_spec = "$local_part\x40$domain";
- return preg_match("!^$addr_spec$!", $email)? 1 : 0;
- }
- if (is_valid_email_address($email)){
- print $email + 'is a valid email address';
- } else{
- print 'Please enter a valid email address';
- };
- function validate_password($password){
- if(!isset($password)){
- print 'Please provide a password';
- }else if(strlen($password) <=5){
- print 'Your password must be at least 5 charachters long';
- }
- else{return $password;}
- };
- function confirm_password($confirm_password){
- if ($password !== $confirm_password){
- print 'Please enter the same password as above';
- }else{ $password === $confirm_password;}
- return $confirm_password;
- }
- };
- $ar=array("1"=>one,"2"=>"two");
- $json=json_encode($ar);
- echo $json;
- function ajax(calledpage, errordiv)
- {
- var xmlhttp;
- if (window.XMLHttpRequest)
- xmlhttp=new XMLHttpRequest();
- else
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- xmlhttp.onreadystatechange=function(){
- if (xmlhttp.readyState==4 && xmlhttp.status==200)
- document.getElementById(errordiv).innerHTML=xmlhttp.responseText;
- }
- xmlhttp.open("GET", calledpage, true);
- xmlhttp.send();
- }
- ajax('mypage.php?x=myvariable','my_error_divs_id');
Add Comment
Please, Sign In to add comment