fubowl Posted October 19, 2010 Share Posted October 19, 2010 Hi, I'm using preg_match to validate various fields. I wish to check that a string the length of four numbers, as a year, eg. 2010. How would I do this, something like: /^[0-9'\s]{4}*$/i ? As usual, I have no idea. Link to comment https://forums.phpfreaks.com/topic/216248-preg_match-with-specified-lenth-question/ Share on other sites More sharing options...
salathe Posted October 19, 2010 Share Posted October 19, 2010 So you want to check that the string is a number between 1000 and 9999? Do you need to use a regex? Link to comment https://forums.phpfreaks.com/topic/216248-preg_match-with-specified-lenth-question/#findComment-1123806 Share on other sites More sharing options...
fubowl Posted October 19, 2010 Author Share Posted October 19, 2010 No but I've set it up to use it as default validation. I mainly just want to check only for numerals and it being 4 digits long. Link to comment https://forums.phpfreaks.com/topic/216248-preg_match-with-specified-lenth-question/#findComment-1123849 Share on other sites More sharing options...
salathe Posted October 19, 2010 Share Posted October 19, 2010 So 0002 is OK? (These Qs might seem silly, but are important) Link to comment https://forums.phpfreaks.com/topic/216248-preg_match-with-specified-lenth-question/#findComment-1123851 Share on other sites More sharing options...
fortnox007 Posted October 19, 2010 Share Posted October 19, 2010 I think you can strip some stuff from your regex /^[0-9]{4}$/ i is not needed, numbers are not case sensitive. the * is not needed (maybe even confusing) more cases can be found by preg_match_all this regex doesn't allow any other characters than 0-9 but i assume you want that. Does this help you? IF you want a certain range like starting from 1900 let me know Link to comment https://forums.phpfreaks.com/topic/216248-preg_match-with-specified-lenth-question/#findComment-1123854 Share on other sites More sharing options...
Pikachu2000 Posted October 19, 2010 Share Posted October 19, 2010 You should only use regex when it's actually needed. Link to comment https://forums.phpfreaks.com/topic/216248-preg_match-with-specified-lenth-question/#findComment-1123874 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.