It goes two places: in the php script that handles the post request and also in a javascript served with the form. Also, you want to match start of line, one or more characters and end of line, so regepx should be:
/[-.,?!()$%A-Za-z0-9]+$/;
In the form, you could add an onkeyup or onchange event for the actual input field to validate input either as it occurs or when the control loses focus with changed data, or you add an onsubmit event to the form to validate all data before the form is submitted.
Also, you will want to add a range for number of matches of the character class
I'm assuming <form onsubmit="return validate();" ...>
function validate() {
var d = document;
var o = d.getElementById('inputfield');
// I added +$ to the regexp. At least one character, and end of the string after the last char.
var re = /^[-.,?!()$%A-Za-z0-9]+$/;
var fail = !re.test(o.value);
if (fail) {
//...
return false;
}
return true;
}
And then pretty much the same thing on the server again.
$re = "/^[-.,?!()$%A-Za-z0-9]+$/";
if (preg_match($re, $_POST['inputfield') == 0) {
// no match
}
else {
// ok
}