views:

37

answers:

1

I am writing some code to give live feedback to the user on the validation of a form using AJAX. I have got it checking length and if the field is empty. Now I want it to sanitize the users input and if the sanatized input differs from the users original input then tell them which characters are not allowed.

The code I have written so far works except some characters most notably a '£' symbol result in no response. I think it relates to json_encode and its encoding.

Here is the code:

  $user_input = 'asdfsfs£';
  $strip_array = str_split(strip($user_input));
  $orig_array = str_split($user_input);
  $diff_array = array_diff($orig_array,$strip_array);
  $diff_str = implode(', ',$diff_array);
  $final = json_encode($diff_str);

  function strip($input){return htmlentities(strip_tags($input),ENT_QUOTES);}

Hope someone can figure out a solution.

A: 

if this is to be done via AJAX then you will need to write a javascript function to accomplish this.

Let the server return a JSON object of the format

{
    original: 'asdfsfs£',
    edited: 'asdfsfs',
    illegalChars: ['£']
}

in your html, you should have empty elements next to the form input fields where you can output the form validation message

<input type="text" id="myTextField" />
<span id="myTextFieldLabel" />

now let your javascript update the content of the myTextFieldLabel component according to the validation data

Sean

seanizer
Maybe I didnt make myself clear, the Ajax is working just fine for validation. The problem is the code I am using for checking for illegal characters (posted above) returns the correct results for > or " but for the '£' it returns nothing. I believe it is to do with the text encoding not being UTF-8 after it has been processed.
JimBo