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. Quote 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? Quote 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. Quote 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) Quote 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 Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.