ThemeForest

Settings API Validating User Input

204 posts Bold WordPress Theme Development
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Interviewed on the Envato Notes blog
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 200 and 499 users
srhnbr says

Hey everyone.

I’m trying to validate comma separated numbers as in the case of asking a WordPress admin to enter category or page IDs in a text input field.

The value entered may or may not include multiple numeric IDs so I need to validate both cases so:

a) 12

b) 123,456,789

someone may enter the comma separated IDs also like so:
c) 123, 456, 789

I thought this would work:

preg_match('/^\d(?:[,]\d)*(?:[,]\s\d)*$/', $input[$option['id']]);

then use the result in a conditional that will pass the value if there is a match to my $valid_input array

but it fails miserably :( I think my regular expession is wrong.

Could anyone help? Please?

1 year ago
2211 posts
  • Has been a member for 1-2 years
  • Exclusive Author
  • Sold between 1 and 100 dollars
  • Bought between 10 and 49 items
  • United States
OrganicBeeMedia says

Hey everyone.

I’m trying to validate comma separated numbers as in the case of asking a WordPress admin to enter category or page IDs in a text input field.

The value entered may or may not include multiple numeric IDs so I need to validate both cases so:

a) 12

b) 123,456,789

someone may enter the comma separated IDs also like so:
c) 123, 456, 789

I thought this would work:

preg_match('/^\d(?:[,]\d)*(?:[,]\s\d)*$/', $input[$option['id']]);

then use the result in a conditional that will pass the value if there is a match to my $valid_input array

but it fails miserably :( I think my regular expession is wrong.

Could anyone help? Please?
http://www.chipbennett.net/2011/02/17/incorporating-the-settings-api-in-wordpress-themes/7/
1 year ago
204 posts Bold WordPress Theme Development
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Interviewed on the Envato Notes blog
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 200 and 499 users
srhnbr says

@OrganicBeeMedia

thank you kindly for the reply but I don’t think you actually read my question ;)

I was not asking how to use the Settings API but how to validate a particular user input!

Anyway I got it figured out. If anyone is interested here’s the correct regular expression

preg_match('/^\d+(?:[,]\d+)*(?:[,]\s\d+)*$/', $input[$option['id']])
1 year ago
by
by
by
by
by