PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Not allowing spaces in fields

  1. I created a form and am using it with the User Registration add-on. I'd like to limit the field for username to now allow spaces.

    I found a tutorial on not allowing certain characters in a field:

    When I try adding that to my functions.php file, it crashed my site, until it's removed. Once that's figured out, how would I adapt the code to not allowing spaces, rather than "86" like in the example.

    Posted 9 years ago on Thursday December 20, 2012 | Permalink
  2. If it crashes your site, you have added it in the wrong place in the file, or you have a syntax error. If you post your complete functions.php to or we can check it for syntax errors. Please post the complete file as it stands when it crashes your site.

    Posted 9 years ago on Friday December 21, 2012 | Permalink
  3. I seemed to have got the code to work for the most part, however, how would I change the validation from "86" in the example to a space?

    I'm trying to use this validation on a field that will be used for a WP username, thus not allowing spaces in the username.

    Posted 9 years ago on Thursday December 27, 2012 | Permalink
  4. David Peralty

    You can't just take that code and use it. You will have to find a way to detect the space character using PHP. There are many tutorials on Google to do just that.

    Posted 9 years ago on Thursday December 27, 2012 | Permalink
  5. A space character is valid in a WordPress username. You might have reasons for not allowing them, but a space character is accepted when creating a WordPress user account.

    However, if you want to return a validation error when a space is entered in your username field, you can check for spaces in the input like this (on line 08):

    // change input_1 here to your field ID
    // \s is any whitespace character
    if ( preg_match("/\s/", $_POST['input_1'] )) {
    Posted 9 years ago on Friday December 28, 2012 | Permalink