Regular expression for validating date format mmddyyyy

Posted by / 18-Aug-2015 07:52

Regular expression for validating date format mmddyyyy

It is very important to validate the data supplied by user through a form, before you process it. In the following examples a Java Script function is used to check a valid date format against a regular expression.

Among various kind of data validation, validation of date is one. Later we take each part of the string supplied by user (i.e.

= null) { println("Success date is: "+ date); } public Date parse(String source) throws Parse Exception { Parse Position pos = new Parse Position(0); Date result = parse(source, pos); if (pos.index == 0) throw new Parse Exception("Unparseable date: \"" + source + "\"" , pos.error Index); return result; }pos.index (Index() from other packages) returning 0, the result Date being null or Error Index() returning a value other than -1 all indicate error.

I'm a great fan of regex but this is not the right place to use it.Especially since the OP's regex allows '-' as a field separator but the Simple Date Format pattern does not!I would not even use a regex to convet the '-' separators to '/'; I would just use String.replace() ./* try { Date date = sdf.parse(input Str); println("Success date is: "+ date); } catch (Parse Exception e) { e.print Stack Trace(); } */ Parse Position position = new Parse Position(0); Date date = sdf.parse(input Str, position); if (date !In this tutorial, we discussed how you can perform Java Script date validation in 1. dd, mm and yyyy) and check whether dd is a valid date, mm is a valid month or yyyy is a valid year.

regular expression for validating date format mmddyyyy-48regular expression for validating date format mmddyyyy-19regular expression for validating date format mmddyyyy-64

We have also check the leap year factor for the month of February.

One thought on “regular expression for validating date format mmddyyyy”