{"id":1356,"date":"2022-08-30T15:15:51","date_gmt":"2022-08-30T15:15:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/13\/2-or-4-digit-date-validation-with-javascript-string-replacement-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:51","modified_gmt":"2022-08-30T15:15:51","slug":"2-or-4-digit-date-validation-with-javascript-string-replacement-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/2-or-4-digit-date-validation-with-javascript-string-replacement-collection-of-common-programming-errors\/","title":{"rendered":"2 or 4 Digit Date Validation with Javascript String Replacement-Collection of common programming errors"},"content":{"rendered":"<p><strong>EDIT: To anyone making the same mistake I did in asking this question, please look for my amended answer below which demonstrates a MUCH cleaner solution to this problem.<\/strong><\/p>\n<p>Trying to permit user entry of either 2 digit or 4 digit date format and allow both to pass validation but autocorrect the 2 digit format with the 4 digit equivalent with the assumption that the leading 2 digits should be the same as the current leading 2 digits.<\/p>\n<p>I&#8217;m having trouble with the javascript <code>replace()<\/code> function. This line:<\/p>\n<pre><code>            input.replace(enteredDate, year);\n<\/code><\/pre>\n<p>is not working as expected. In the context of the <code>spine.js<\/code> framework I&#8217;m using, the <code>input<\/code> refers to <code>$(event.target)<\/code> and appears to be correct everywhere else in manipulating the <code>input<\/code> field where the validation should be occurring. Ultimately, I want to replace the original string found in the input field with the fully concatenated one (which I haven&#8217;t built out yet), but for now, how can I replace the contents of <code>input<\/code> (where input is <code>var = $(event.target)<\/code>) with the var <code>year<\/code> assigned here:<\/p>\n<p><code>var year = currentYear.charAt(0) + currentYear.charAt(1) + unparsedYear;<\/code><\/p>\n<p>Here is the full code for the function I have so far, which is throwing the following error in reference to the <code>.replace()<\/code> line &#8211; <code>Uncaught TypeError: Object [object Object] has no method 'replace'<\/code><\/p>\n<pre><code>validateDate: function(event) {\n\n        var input = $(event.target);\n        var enteredDate = input.val();\n\n        input.destroyValidationMessage();\n\n        var pattern = \/^(\\d{1,2})\\\/(\\d{1,2})\\\/(\\d{2})|(\\d{4})$\/;\n        var result = enteredDate.match(pattern);\n\n        if (result !== null) {\n            var month = parseInt(result[1], 10);\n            var day = parseInt(result[2], 10);\n            var year = parseInt(result[3], 10);\n\n            var unparsedYear = parseInt(result[3], 10);\n            var unparsedYearStrLength = unparsedYear.toString().length;\n            if ( unparsedYearStrLength &lt; 4) {\n                var currentYear = new Date().getFullYear().toString();\n\n                var year = currentYear.charAt(0) + currentYear.charAt(1) + unparsedYear;\n                alert('Autocorrected year will be ' + year);\n                input.replace(enteredDate, year);\n            }\n\n            var date = new Date(year, month - 1, day);\n            var isValid = date.getFullYear() === year &amp;&amp; date.getMonth() === month - 1 &amp;&amp; date.getDate() === day;\n\n        } else {\n            input.createValidationMessage('Invalid Date');\n            input.addClass('invalid'); \n        }\n    }\n<\/code><\/pre>\n<ol>\n<li>\n<p>As mentioned in my comment above:<\/p>\n<p><code>input<\/code> is a DOM element, but you actually want to replace the <em>content<\/em> of the element vs. the element itself, so use <code>input.val(new_value)<\/code> and not the actual <code>input<\/code> element.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-13 09:49:22. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>EDIT: To anyone making the same mistake I did in asking this question, please look for my amended answer below which demonstrates a MUCH cleaner solution to this problem. Trying to permit user entry of either 2 digit or 4 digit date format and allow both to pass validation but autocorrect the 2 digit format [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1356","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1356"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1356\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}