Title: sanitize imput=data
Last modified: September 9, 2023

---

# sanitize imput=data

 *  [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/)
 * I’d like to know how can I sanitize 2 custom fields with imput=data like this:
 *     ```wp-block-code
       update_user_meta( $user_id, 'prezzi_1da', sanitize_meta( $_POST['prezzi_1da'] ) );
   
       update_user_meta( $user_id, 'prezzi_1a', sanitize_meta( $_POST['prezzi_1a '] ) );
       ```
   
 * sanitize_meta is not good, sanitize_text_field neither, sanitize_textarea neither.
   What can I use? I want the date I pick stay visible. It’s NOT a date range. It
   is as if it were a number field. It’s just to write a date more quicky than editing
   in the way i.e. 09/16/2024

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Anonymous User 20287723
 * (@anonymized-20287723)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17037921)
 * Hello! Are you saying this is a field like this:
 *     ```wp-block-code
       <input type="date" name="prezzi_1da"
       ```
   
 * Or is it:
 *     ```wp-block-code
       <input type="text" name="prezzi_1da"
       ```
   
 * Or maybe it’s datetime or datetime-local? It’d help to know what type of field
   you’re using, first.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17037988)
 * These are the fields:
 *     ```wp-block-code
       <td>
                      <input type="date" id="periodo_1da" name="periodo_1da" class="list_prezzi" ><?php echo esc_attr( get_the_author_meta( 'periodo_1da', $user->ID ) ); ?><br/>
                       <span class="locazione">prezzo dal</span>
                   </td>
        <td>
                      <input type="date" id="periodo_1a" name="periodo_1a" class="list_prezzi" ><?php echo esc_attr( get_the_author_meta( 'periodo_1a', $user->ID ) ); ?><br/>
                       <span class="locazione">al</span>
                   </td>
       ```
   
 * My purpose is to write a date range for a list price, i.e.: 9/9/2023-16/9/2023:
   400 €. From the author (agency) level I manage the periods and from the post (
   apartment) level I manage the prices. But I dont want to save the meta data as
   date range, I just write a date than another date
 *  Anonymous User 20287723
 * (@anonymized-20287723)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17037991)
 * “I just write a date than another date”
 * This is the part you need to explain more fully, please. I’m not on your project
   so you need to start at the beginning and be detailed. Maybe write a lot in Italian
   and I will use a translator.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17038111)
 * there is not a general rule to sanitize date fields? Something like sanitize_text_field
   for a text field
 *  Anonymous User 20287723
 * (@anonymized-20287723)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17038117)
 * [Date type fields](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date)
   can be sanitized using [sanitize_text_field](https://developer.wordpress.org/reference/functions/sanitize_text_field/).
   They transmit a string. So why not?
 * Without seeing all your code and understanding what you’re trying to do, it’s
   hard to help further.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17038132)
 * If the only allowed characters are `0-9`, `-` or `/`, you could validate and 
   sanitize with something like:
    `$periodo_1da = preg_filter('![^0-9-/]+!', '',
   $_POST['periodo_1da']);` This strips out any characters that match the regex.
   The `^` regex symbol is for negation, so it’s saying match anything that is not
   listed. Thus you can add or remove any characters you like that should be retained
   or stripped accordingly.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17038198)
 * I’m getting one output but out of the date picker [https://ibb.co/JckkfPS](https://ibb.co/JckkfPS)
 *     ```wp-block-code
       update_user_meta( $user_id, 'periodo_1da', sanitize_text_field( $_POST['periodo_1da'] ) );
   
       update_user_meta( $user_id, 'periodo_1a', sanitize_text_field( $_POST['periodo_1a '] ) );
       ```
   
 * but now I’m getting nothing, even using
 *     ```wp-block-code
       $periodo_1da = preg_filter('![^0-9-/]+!', '', $_POST['periodo_1da']);
       ```
   
 * Maybe the problem is in my fields?
 *     ```wp-block-code
       <td>
                      <input type="date" id="periodo_1da" name="periodo_1da" class="list_prezzi" ><?php echo esc_attr( get_the_author_meta( 'periodo_1da', $user->ID ) ); ?><br/>
                       <span class="da">prezzo dal</span>
                   </td>
        <td>
                      <input type="date" id="periodo_1a" name="periodo_1a" class="list_prezzi" ><?php echo esc_attr( get_the_author_meta( 'periodo_1a', $user->ID ) ); ?><br/>
                       <span class="a">al</span>
                   </td>
       ```
   
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17038781)
 * I’va done today what I did yesterday but with a simple text field: [https://ibb.co/hWqjZY5](https://ibb.co/hWqjZY5)
 * The result is the same. I though it was more confortable to insert the periods
   on calendars, this is not so sure. Anyway I’d like to learn how to use my first
   method (with calendars), what I need is sanitizing the fields and get the dates
   fixed somewhere
 * The result is : [https://ibb.co/wJTC2tj](https://ibb.co/wJTC2tj)
 * A period managed from author (agency) position, and a price managed from post(
   apartment) position
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17039269)
 * Did you try this:
    `update_user_meta( $user_id, 'periodo_1da', preg_filter('![
   ^0-9-/]+!', '', $_POST['periodo_1da'] ) );` ?
 * Fields look OK, all that really matters is the type and name attributes.
 * You use `$user_id` in one place and `$user->ID` in another. It may be correct,
   but it looks a little suspicious. Double check that you’re saving data with the
   same ID value that you’re getting data with.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘sanitize imput=data’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 9 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 9 months ago](https://wordpress.org/support/topic/sanitize-imputdata/#post-17039269)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
