Hello,
I am trying to validate and mask a text box that should contain a
DateTime in the following format:
yyyy-mm-dd hh:mm:ss
The validation is not working as expected. The problem is:
In a presence of a valid data I deleted a small part of it (ss). The
mask is revealed for this part.
If after deleting a part of the date/time I change the focus to
another input or submit the form I get:
1. A message saying the date/time is invalid (This is expected)
2. The entire content of the input disappears. (This is not suppose
to happen!)
Every time I insert a invalid date I get a message but the date/time
inserted is deleted. What is going on?
I am testing the date/time in 2 ways: Required and through a method
that uses Regex:
// Form validation
$("#Edit").validate({
rules: {
UpdatedAt: {
required: true,
sqldatetime: true
}
},
messages: {
UpdatedAt: {
required: "Insert Date/Time",
sqldatetime: "Check the date/time. Use the format yyyy-mm-
dd hh:mm:ss"
}
}
});
And the method sqldatetime is:
$.validator.addMethod('sqldatetime', function (value) {
return /^(([0-9]{4})-([0-1][0-9])-([0-3][0-9])\s([0-1][0-9]|[2]
[0-3]):([0-5][0-9]):([0-5][0-9])|)$/.test(value);
}, 'Verifique a data e hora. Use o formato yyyy-mm-dd hh:mm:ss');
The Regex validates only non empty values. If the value is empty it is
also valid.
I have made this so that my method does not conflict with Required and
allow to have the two options only by adding or not the required rule.
I tried to find the problem but wasn't able to.
Any idea what is going wrong?
Thanks,
Miguel