1

I would need to validate my date. But when I key in 01132014 , it doesn't show any error message and it prints out 01 Jan, 2015 Also when I key in 06035555, it doesn't print out any error and continue to show 06 Mar 5555

But if I key in weird dates like 33333333, it does prints out "Your input has no logic at all"
Additionally, I do not want the error message to pop up an alert box. I want the error message to be shown on the textbox itself. How do I do it?

May I know what should I add in to validate? I tried to view other answers but still, it's not working.

Javascript:

    function parseDate(s) {

    if (isNaN(Date.parse(s))) {
            alert("Your input has no logic at all");
            return;
        }

        var reg = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g;

        var dateParts = s.split("/");

        var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

        var dateStrParts = date.toString().split(" ");

        return (date.getDate() + ", " + dateStrParts[1] + " " + dateStrParts[3]);
    }
</script>

Calendar Tag:

    <p:calendar 
    value="#{pc_Rpt2202.asat_date}"
    id="rp2202_input_as_at" 
    styleClass="calendar" 
    maxlength="10"
    pattern="dd MMM yyyy"
    onchange="$(this).val(parseDate($(this).val()))"
    onfocus="$(this).mask('99/99/9999');"
>
    <p:watermark for="rp2202_input_as_at" value="dd/MMM/yyyy" />
    <f:convertDateTime pattern="dd MMM yyyy" />
</p:calendar>
isg
  • 39
  • 10
  • What is the pattern of the value in your `s` parameter? – Zaenille Jun 18 '14 at 02:25
  • @MarkGabriel a string, `s.split("/")` you mean this pattern? Sorry I am weak in understand java languages so there will be times where I am confuse... Really sorry! – isg Jun 18 '14 at 02:31
  • http://stackoverflow.com/questions/14693298/js-check-for-valid-date-format/28777878#28777878 – Sony Mathew Mar 06 '15 at 07:19

1 Answers1

0

Ok, two things. First, Date.parse tends to be implementation specific and so I wouldn't use it. And second, basically regex is a pattern validator, not a content validator. You best bet is to keep the regex simple, something like /^(0?[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19|20)[0-9][0-9]$/ and then afterwards split and check the content of the pieces, like so:

var re = /^(0?[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19|20)[0-9][0-9]$/;
if (!re.test(s)) {
        $('#rp2202_input_as_at').val("Your input has no logic at all");
        return;
    }
 var dateParts...etc.
Mike
  • 1,199
  • 1
  • 15
  • 24
  • Hi. I tried your regular expression but now it doesn't seem to print out any error when I key in **31022015** (it returns 3 March 2015) and **33333333** (it returns 3 Oct, 3335) – isg Jun 18 '14 at 04:11
  • @isg - try it now, forgot to put in the (s). – Mike Jun 18 '14 at 04:15
  • Oh wait! I added in the (s) and it's working! But how do I print the error message such that it shows on the textbox instead on popping up an alert box? – isg Jun 18 '14 at 04:20
  • @isg - edited the question to change the input box value to the error message. – Mike Jun 18 '14 at 04:35