Passing Special Characters to PHP via AJAX -


i'm collecting form data, sending php validation script through ajax call. issue on special characters php validation script not working expected.

html:

<input type="text" name="firstname" class="firstname"        placeholder="[first name]" required autofocus maxlength="25" size="25" /> 

js:

$(".button").click(function () {     var firstname = encodeuricomponent($("input.firstname").val());     var datastring = "firstname=" + firstname;      $.ajax({         type: "post",         url: "/scripts/validatesignup.php",         data: datastring,         cache: false,         success: function (errormessage) {             //print screen         }     }); }); 

php validation

$postdata = $_post;  if (filter::validatestring($postdata['firstname']) == false) {     echo "oops! characters used in first name not valid."; } 

php filter

//returns true if string good, false otherwise public static function validatestring($string) {     $string = trim($string);      if ($string == null || $string == "") {         return false;     } else {         if (preg_match("/[^\.\,\-\_\'\"\@\?\!\:\;\$\#\%\&\+\= a-za-z0-9()]/", $string) == true) {             return false;         } else {             return true;         }     } } 

on empty string prints error screen fine. if "~!@#$%^&*()", accepts string , doesnt throw , error, though result of preg_match == false.

$string = trim($string);  if ($string == null || $string == "") {     return false; } else {     if (preg_match("/[^\.,\-_'\"@?!:;\$#&\+=\sa-za-z0-9\(\)]/", $string) == true) {         return false;     } else {         return true;     } } 

that more valid regex, not result want: you're checking pretty input, it'll match "abcd" , return false well. there 11 characters special meanings regular expressions, , " need escaped: ^$[]()|.*+-


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -