tags:

views:

29

answers:

1

Is there a php function to get regular expressions able to check if an input fits a certain MySQL data type?

In example:

$foo = get_regex_for_data_type("int(10) unsigned");
echo $foo;

would return something like:

/^[0-9]{1,10}$/
+1  A: 

No, there is not a PHP function to do that. You could write it yourself, but it would be very difficult to get it 100% right. For example, 0xFF is a perfectly valid value to insert into that field, but your regex would exclude it.

Perhaps there is another way to accomplish what you're trying to do? Is the problem that you're worried a value might be too large for the field?

Eli
I want to validate input for database fields in php and javascript. Is there some library to do that?
Thomas
Yes, many. Perhaps this is what you're looking for: http://validformbuilder.org/
Eli
You're still going to have to tell it what you expect for input for each field. I'm not aware of any libraries that are able to infer that from the schema.
Eli
I already did the php/js part, i only need the translation of fieldtypes into regular expressions.
Thomas