LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2019, 10:40 AM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,668

Rep: Reputation: 134Reputation: 134
Need working date pattern for html <input>


I've tested various patterns, including going to http://html5pattern.com/Dates. No patterns work perfectly. The closest I've found is
Code:
<input type="text" placeholder="mm/dd/yyyy"
pattern="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d"/>
but this one requires a 2 digit month, e.g. "01", instead of simply "1", and it requires a 4 digit year. For the latter, I'd like it to accept either 4 digits, or the last two digits only.

Does anyone know how I can modify the pattern to give me what I want?

Perhaps more importantly, is there a tutorial out there somewhere on these pattern regex's? I haven't been able to find anything.
 
Old 10-08-2019, 11:57 AM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,778

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
On html forms, I generally do dates one of two ways:

Use drop-down lists for each field (DD MM YYYY). Cumbersome, and almost requires javascript to be sure the day list matches the month
or use
Code:
<input-type="date" >
or 
<input-type="datetime-local">
which may give you a date-picker

See: https://www.w3schools.com/html/html_...nput_types.asp

That said, I was unaware that one could do what you're doing with html...I'll have to play with that.
I don't know which regex that is using. Looking forward to other responses.

There are many excellent tutorials about regular expressions/regex out there.
This site will validate your regex on the fly:
https://www.regextester.com/22
I think [1-12] will match month without leading zeros, but I've not tested that.
 
Old 10-08-2019, 08:00 PM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,652

Rep: Reputation: Disabled
Quote:
Originally Posted by mfoley View Post
but this one requires a 2 digit month, e.g. "01", instead of simply "1", and it requires a 4 digit year. For the latter, I'd like it to accept either 4 digits, or the last two digits only.
This comes pretty close:
Code:
<input type="text" placeholder="mm/dd/yyyy" pattern="([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]((19|20)\d\d|\d\d)"/>
...but you still have the issue of specific months having different number of days, and then of course there's the issue with leap years. As mentioned in the post above, the "date" input type is usually better suited for this task.

Keep in mind that no matter what you decide to do on the client side, the date (as any user input) must always be validated by logic on the server side. Formatted input fields and JavaScript are nice and will keep users from entering invalid data by mistake, but there's nothing stopping someone from manually submitting whatever data they choose directly to the server.
 
  


Reply

Tags
html5, pattern


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
substituting pattern only if another pattern is present rattlesnakejoe Programming 4 07-22-2009 07:46 PM
substitute a pattern only if some other pattern is present raghu123 Programming 3 06-26-2009 06:53 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration