Support

Account

Home Forums Front-end Issues Front End Form Special Characters Validation

Helping

Front End Form Special Characters Validation

  • Hi guys,

    I have a front end form that allow a few fie uploads. At the moment when a user uploads a file with a special character in the file name it causes an error 500 to trigger.

    I am looking to validate that the filename doesnt include any special characters prior to allowing the form to submit and allow the upload but am stuck.

    I have used the acf/validate_attachment hook but no luck. It works when using the example and posts the error but cant get it to work to check the filename for special characters.

    Code I have so far:

    add_filter('acf/validate_attachment', 'my_acf_validate_attachment', 10, 5);
    function my_acf_validate_attachment( $errors, $file, $attachment, $field, $context ){
    
    if( preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $attachment['name']) ) {
    $errors[] = __( 'File name must begin with "acf-"' );
    }
    return $errors;
    }

    Any help would be much appreciated.

  • I don’t know if this is the cause, but your expression could be causing issues. many of the characters you are searching for need to be escaped

    
    preg_match('/[\'^£\$%&\*\(\)\}\{@#~\?><>,\|\=_\+¬-]/', $attachment['name']) )
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.