Problem with Special Characters

flinx777flinx777 Member
in General edited August 2007
So I'm sure this is probably an easy Javascript code (unfortunately I really don't know Javascript), but I'm trying to remove special characters from a template variable's output using MODx. This problem will obviously not be unique to MODx, but will be a problem for any customer that uses a CMS and Foxy Cart. The obvious solution is to tell people not to put special characters in, but as we all know, it's bound to happen. In this case, I want to remove any special characters the end user may input. When someone enters information into "Title" field in the document (using MODx), I want the output (in this case, the [*pagetitle*] T.V. as seen below) to remove the special characters (i.e. ', #, *, ", etc.). See the problem live at: http://www.cherubscottage.com/index.php?id=104

Here's my code below:









I was playing with some Javascript, trying to modify it (but again, I know really nothing of Javascript), but here's the basic idea:

function filterNum(str) {
re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\|\?|\||\\|\!|\$|\./g;
// remove special characters like "$" and "," etc...
return str.replace(re, "");
}

Is there a way in Foxy Cart to remove special characters that an end user of any CMS might input?

Thanks!
Comments
  • lukeluke FoxyCart Team
    Brett is the regex master, so I'll let him tackle this one. Form data validation is a key part of any business process so we don't want to get involved in doing it ourselves if one company needs information that another company wants to strip out. We'll get you some validation code that should work well.
  • flinx777flinx777 Member
    edited August 2007
    Thanks Luke for the update on that...I'll await Brett's suggestions.
  • brettbrett FoxyCart Team
    edited August 2007
    Hey flinx.

    Javascript isn't the answer for something like this. You're talking about manipulating data that a store admin inputs (in a CMS like MODx) before it's submitted to FoxyCart. While javascript could work, it wouldn't be the best method.

    What I'd do would be to use PHx in your output, and create a PHx modifier to strip what you want. So you'd call your form with [*option:myPHxThing*]. Check the MODx wiki for more info:
    http://wiki.modxcms.com/index.php/PHx#Creating_a_custom_modifier

    You might want to take a look at a replacement array in the Custom Alias plugin (for MODx):
    http://modxcms.com/Custom-Alias-1551.html
    Check that code and notice the massive replacement array. It's probably more than you need, but you should be able to use that idea and modify it for a custom PHx modifier.

    Edit: Man, we really have to fix the forum formatting to auto-link things. Right after the new admin ;)
  • Hey Brett,

    Thanks for the advice...I'll check it out and let you know how it works. Thanks again for your insight...and yes, autolinking would be nice... ;)
  • So after what seems forever ;) I was finally able to get this working. My notes below are using MODx by the way.

    In my form I put:



    with the snippet cleanTV of:



    Hope that helps someone.
  • lukeluke FoxyCart Team
    Good stuff flinx, thanks for sharing.
Sign In or Register to comment.