-7

when user submit a form input text

i want it just to allow a-z 0-9 and -_ just this numbers digits and -_

how do i check the input and make it only this inputs allows and delete the rest

Thank You!

3 Answers 3

1
$output = preg_replace("/[^a-zA-Z0-9_\-]/", '', $input);
2
  • No need to escape the -, as it's the last item in the list. No harm done, though. You could also specify just a-z or A-Z and use the "i" flag.
    – Phil Perry
    Jan 27, 2014 at 19:09
  • Ah, thanks. Actually, the first version of that answer the - was not escaped. Then I doubted myself and went back to add without testing. :) Forgot about the "i" flag. :)
    – TunaMaxx
    Jan 27, 2014 at 19:13
1
function validate($value){
    return preg_match("/^[a-zA-Z0-9_\-]*$/", $value) !== 0;
}

If the return value is false then you have invalid characters, if it true, everything is correct.

1
$input = preg_replace("/[^0-9a-z\_\-]/i", "", $_POST["myfield"]);

Use regexp.

Not the answer you're looking for? Browse other questions tagged or ask your own question.