Reg Exp for Date in format MM/YY
I have mask for my date in MM/YY format. There must be month >= then current, and year >= then current year.
I have regexp pattern here:
<input id="expiration" type="tel" placeholder="MM/YY" class="masked" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" data-valid-example="12/18" onchange="onChangeInput(event)"/>
Seems like it's ok for this format, but I still can write a year less than 18.
Need a little help from you, stack overflow.
javascript regex date
add a comment |
I have mask for my date in MM/YY format. There must be month >= then current, and year >= then current year.
I have regexp pattern here:
<input id="expiration" type="tel" placeholder="MM/YY" class="masked" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" data-valid-example="12/18" onchange="onChangeInput(event)"/>
Seems like it's ok for this format, but I still can write a year less than 18.
Need a little help from you, stack overflow.
javascript regex date
Seems like he's okDon't think so, parentheses are imbalanced
– CertainPerformance
Nov 23 '18 at 10:04
3
Please add the whole relevant code to the question.
– Wiktor Stribiżew
Nov 23 '18 at 10:05
2
Please add the code to the question by editing the question, not by dumping it in a comment.
– Dragonthoughts
Nov 23 '18 at 10:14
1
in your comment, the input type istel. According to the HTML5 specification,<input type="tel">is used for input fields that should contain a telephone number. The<input type="month">allows the user to select a month and year. Depending on browser support, a date picker can show up in the input field. I think this is more appropriate than trying to mask a telephone number...
– Dragonthoughts
Nov 23 '18 at 10:17
Man, I have different task. I've asked only about Reg Exp. This pattern is ok or not for the this kind of date. Like current or more then current month and year
– Tumanovskiy Andrew
Nov 23 '18 at 10:26
add a comment |
I have mask for my date in MM/YY format. There must be month >= then current, and year >= then current year.
I have regexp pattern here:
<input id="expiration" type="tel" placeholder="MM/YY" class="masked" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" data-valid-example="12/18" onchange="onChangeInput(event)"/>
Seems like it's ok for this format, but I still can write a year less than 18.
Need a little help from you, stack overflow.
javascript regex date
I have mask for my date in MM/YY format. There must be month >= then current, and year >= then current year.
I have regexp pattern here:
<input id="expiration" type="tel" placeholder="MM/YY" class="masked" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" data-valid-example="12/18" onchange="onChangeInput(event)"/>
Seems like it's ok for this format, but I still can write a year less than 18.
Need a little help from you, stack overflow.
javascript regex date
javascript regex date
edited Nov 23 '18 at 10:28
Tumanovskiy Andrew
asked Nov 23 '18 at 10:02
Tumanovskiy AndrewTumanovskiy Andrew
12
12
Seems like he's okDon't think so, parentheses are imbalanced
– CertainPerformance
Nov 23 '18 at 10:04
3
Please add the whole relevant code to the question.
– Wiktor Stribiżew
Nov 23 '18 at 10:05
2
Please add the code to the question by editing the question, not by dumping it in a comment.
– Dragonthoughts
Nov 23 '18 at 10:14
1
in your comment, the input type istel. According to the HTML5 specification,<input type="tel">is used for input fields that should contain a telephone number. The<input type="month">allows the user to select a month and year. Depending on browser support, a date picker can show up in the input field. I think this is more appropriate than trying to mask a telephone number...
– Dragonthoughts
Nov 23 '18 at 10:17
Man, I have different task. I've asked only about Reg Exp. This pattern is ok or not for the this kind of date. Like current or more then current month and year
– Tumanovskiy Andrew
Nov 23 '18 at 10:26
add a comment |
Seems like he's okDon't think so, parentheses are imbalanced
– CertainPerformance
Nov 23 '18 at 10:04
3
Please add the whole relevant code to the question.
– Wiktor Stribiżew
Nov 23 '18 at 10:05
2
Please add the code to the question by editing the question, not by dumping it in a comment.
– Dragonthoughts
Nov 23 '18 at 10:14
1
in your comment, the input type istel. According to the HTML5 specification,<input type="tel">is used for input fields that should contain a telephone number. The<input type="month">allows the user to select a month and year. Depending on browser support, a date picker can show up in the input field. I think this is more appropriate than trying to mask a telephone number...
– Dragonthoughts
Nov 23 '18 at 10:17
Man, I have different task. I've asked only about Reg Exp. This pattern is ok or not for the this kind of date. Like current or more then current month and year
– Tumanovskiy Andrew
Nov 23 '18 at 10:26
Seems like he's ok Don't think so, parentheses are imbalanced– CertainPerformance
Nov 23 '18 at 10:04
Seems like he's ok Don't think so, parentheses are imbalanced– CertainPerformance
Nov 23 '18 at 10:04
3
3
Please add the whole relevant code to the question.
– Wiktor Stribiżew
Nov 23 '18 at 10:05
Please add the whole relevant code to the question.
– Wiktor Stribiżew
Nov 23 '18 at 10:05
2
2
Please add the code to the question by editing the question, not by dumping it in a comment.
– Dragonthoughts
Nov 23 '18 at 10:14
Please add the code to the question by editing the question, not by dumping it in a comment.
– Dragonthoughts
Nov 23 '18 at 10:14
1
1
in your comment, the input type is
tel. According to the HTML5 specification, <input type="tel"> is used for input fields that should contain a telephone number. The <input type="month"> allows the user to select a month and year. Depending on browser support, a date picker can show up in the input field. I think this is more appropriate than trying to mask a telephone number...– Dragonthoughts
Nov 23 '18 at 10:17
in your comment, the input type is
tel. According to the HTML5 specification, <input type="tel"> is used for input fields that should contain a telephone number. The <input type="month"> allows the user to select a month and year. Depending on browser support, a date picker can show up in the input field. I think this is more appropriate than trying to mask a telephone number...– Dragonthoughts
Nov 23 '18 at 10:17
Man, I have different task. I've asked only about Reg Exp. This pattern is ok or not for the this kind of date. Like current or more then current month and year
– Tumanovskiy Andrew
Nov 23 '18 at 10:26
Man, I have different task. I've asked only about Reg Exp. This pattern is ok or not for the this kind of date. Like current or more then current month and year
– Tumanovskiy Andrew
Nov 23 '18 at 10:26
add a comment |
1 Answer
1
active
oldest
votes
The pattern seems fine, however as noted using an input type tel is likely not a good idea as it's semantically inconsistent.
You might try using script instead of relying on browser validation as support is patchy. Something like the following may suit.
function validateInput(evt) {
var re = new RegExp(this.pattern);
var s = this.value;
// Show whether value is valid or not when input is full
document.querySelector('#s0').textContent = s.length < 5 || re.test(s)? '' : 'Invalid';
}
window.onload = function(){
document.querySelector('#i0').addEventListener('input',validateInput, false);
};<input id="i0" type="text" placeholder="MM/YY" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" maxlength="5"><br>
<span id="s0"></span>add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444491%2freg-exp-for-date-in-format-mm-yy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The pattern seems fine, however as noted using an input type tel is likely not a good idea as it's semantically inconsistent.
You might try using script instead of relying on browser validation as support is patchy. Something like the following may suit.
function validateInput(evt) {
var re = new RegExp(this.pattern);
var s = this.value;
// Show whether value is valid or not when input is full
document.querySelector('#s0').textContent = s.length < 5 || re.test(s)? '' : 'Invalid';
}
window.onload = function(){
document.querySelector('#i0').addEventListener('input',validateInput, false);
};<input id="i0" type="text" placeholder="MM/YY" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" maxlength="5"><br>
<span id="s0"></span>add a comment |
The pattern seems fine, however as noted using an input type tel is likely not a good idea as it's semantically inconsistent.
You might try using script instead of relying on browser validation as support is patchy. Something like the following may suit.
function validateInput(evt) {
var re = new RegExp(this.pattern);
var s = this.value;
// Show whether value is valid or not when input is full
document.querySelector('#s0').textContent = s.length < 5 || re.test(s)? '' : 'Invalid';
}
window.onload = function(){
document.querySelector('#i0').addEventListener('input',validateInput, false);
};<input id="i0" type="text" placeholder="MM/YY" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" maxlength="5"><br>
<span id="s0"></span>add a comment |
The pattern seems fine, however as noted using an input type tel is likely not a good idea as it's semantically inconsistent.
You might try using script instead of relying on browser validation as support is patchy. Something like the following may suit.
function validateInput(evt) {
var re = new RegExp(this.pattern);
var s = this.value;
// Show whether value is valid or not when input is full
document.querySelector('#s0').textContent = s.length < 5 || re.test(s)? '' : 'Invalid';
}
window.onload = function(){
document.querySelector('#i0').addEventListener('input',validateInput, false);
};<input id="i0" type="text" placeholder="MM/YY" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" maxlength="5"><br>
<span id="s0"></span>The pattern seems fine, however as noted using an input type tel is likely not a good idea as it's semantically inconsistent.
You might try using script instead of relying on browser validation as support is patchy. Something like the following may suit.
function validateInput(evt) {
var re = new RegExp(this.pattern);
var s = this.value;
// Show whether value is valid or not when input is full
document.querySelector('#s0').textContent = s.length < 5 || re.test(s)? '' : 'Invalid';
}
window.onload = function(){
document.querySelector('#i0').addEventListener('input',validateInput, false);
};<input id="i0" type="text" placeholder="MM/YY" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" maxlength="5"><br>
<span id="s0"></span>function validateInput(evt) {
var re = new RegExp(this.pattern);
var s = this.value;
// Show whether value is valid or not when input is full
document.querySelector('#s0').textContent = s.length < 5 || re.test(s)? '' : 'Invalid';
}
window.onload = function(){
document.querySelector('#i0').addEventListener('input',validateInput, false);
};<input id="i0" type="text" placeholder="MM/YY" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" maxlength="5"><br>
<span id="s0"></span>function validateInput(evt) {
var re = new RegExp(this.pattern);
var s = this.value;
// Show whether value is valid or not when input is full
document.querySelector('#s0').textContent = s.length < 5 || re.test(s)? '' : 'Invalid';
}
window.onload = function(){
document.querySelector('#i0').addEventListener('input',validateInput, false);
};<input id="i0" type="text" placeholder="MM/YY" pattern="(1[0-2]|0[1-9])/(1[8-9]|2d)" maxlength="5"><br>
<span id="s0"></span>answered Nov 23 '18 at 13:06
RobGRobG
99.3k19111146
99.3k19111146
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444491%2freg-exp-for-date-in-format-mm-yy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Seems like he's okDon't think so, parentheses are imbalanced– CertainPerformance
Nov 23 '18 at 10:04
3
Please add the whole relevant code to the question.
– Wiktor Stribiżew
Nov 23 '18 at 10:05
2
Please add the code to the question by editing the question, not by dumping it in a comment.
– Dragonthoughts
Nov 23 '18 at 10:14
1
in your comment, the input type is
tel. According to the HTML5 specification,<input type="tel">is used for input fields that should contain a telephone number. The<input type="month">allows the user to select a month and year. Depending on browser support, a date picker can show up in the input field. I think this is more appropriate than trying to mask a telephone number...– Dragonthoughts
Nov 23 '18 at 10:17
Man, I have different task. I've asked only about Reg Exp. This pattern is ok or not for the this kind of date. Like current or more then current month and year
– Tumanovskiy Andrew
Nov 23 '18 at 10:26