khhalid Posted July 7, 2010 Share Posted July 7, 2010 hi Guys, I have html <input type = 'text'...> field where user can enter date. Is there a way to make sure a valid date has been entered and the format is 'dd'/mm/yyyy'. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/ Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 You could use regex: if (preg_match('/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}$/', $date)) { echo 'valid date: ' . $date; } Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082407 Share on other sites More sharing options...
tomtimms Posted July 7, 2010 Share Posted July 7, 2010 There are a variety of ways to do this, you can use javascript to make sure an alert is shown that the date is not valid or you can use php/ajax to show an error once the form is submitted that the data is not valid. What are you looking to do with this? Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082408 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 Given the forum this is posted in, I'm going to assume he means PHP. Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082412 Share on other sites More sharing options...
khhalid Posted July 7, 2010 Author Share Posted July 7, 2010 I am using HTML with JavaScript (JS). Any JS example will be very helpful, please. Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082431 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 So you posted in the 'PHP coding help' section? Okay then, well you can still use regex: if (yourDate.match(/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}$/)) { // valid date } Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082436 Share on other sites More sharing options...
harristweed Posted July 7, 2010 Share Posted July 7, 2010 Well MrAdam just shows you should never assume! Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082439 Share on other sites More sharing options...
khhalid Posted July 7, 2010 Author Share Posted July 7, 2010 Sorry MrAdam, I'm using HTML, JScript & PHP together. I will try your solution, thanks; Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082458 Share on other sites More sharing options...
Mchl Posted July 7, 2010 Share Posted July 7, 2010 You should always check it again on server side anyway. Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082461 Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 You should always check it again on server side anyway. Server-side validation = must have Client-side validation = nice to have Quote Link to comment https://forums.phpfreaks.com/topic/207000-isdate/#findComment-1082494 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.