Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript method to ensure that a date is valid
    primarykey
    data
    text
    <p>I'm trying to write a method that checks whether a date is valid. It is passed in three Strings: month, day, and year, in order. Month should be from 0-11 instead of 1-12. I have tested the logic of the code in Java and it works. is_int is another method that tests if a String is composed solely of numerical characters. Unfortunately, I am running into problems which I can't figure out.</p> <pre><code>function is_int(value) { for (i = 0 ; i &lt; value.length ; i++) { if ((value.charAt(i) &lt; '0') || (value.charAt(i) &gt; '9')) return false } return true; } function isValidDate(value1:String, value2:String, value3:String) { if (!is_int(value3)) return false; if (!is_int(value2)) return false; if (!is_int(value1)) return false; var v1 = parseInt(value1) + 1; var v2 = parseInt(value2); var v3 = parseInt(value3); if (v1 &gt; 12 || v1 &lt; 1) return false; if (v2 &gt; 31 || v2 &lt; 1) return false; if (v2 == 31) if (v1 == 2 || v1 == 4 || v1 == 6 || v1 == 9 || v1 == 11) return false; if (v1 != 2) return true; if (v2 &lt; 29) return true; if (v2 == 30) return false; if (v3 % 400 == 0) return true; else if (v3 % 100 == 0) return false; else if (v3 % 4 == 0) return true; else return false; } </code></pre> <p>My first tester is something that asks for three text inputs and, if the isValidDate function returns false, displays an alert. If not, it forwards to a blank html page. However, I tried this:</p> <pre><code>function validate() { if (!isValidDate("a", "a", "a")) { alert("wrong"); return false; } } </code></pre> <p>and the alert never displayed and it forwarded every time. Strangely enough, this still happened when I removed the exclamation point in front of isValidDate. I also tried swapping the double quotation marks for single, but that didn't fix the problem either. The same thing happens with my tester for is_int. I have no idea where I'm going wrong.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload