I have a script that validates date in mm / dd / yyyy format but I would like to change this validation in dd / mm / yyyy format.
SCRIPT
function validarData(fld) {
var testMo, testDay, testYr, inpMo, inpDay, inpYr, msg
var inp = fld.value
var testDate = new Date(inp)
testMo = testDate.getMonth() + 1
testDay = testDate.getDate()
testYr = testDate.getFullYear()
inpMo = parseInt(inp.substring(0, inp.indexOf("/")), 10)
inpDay = parseInt(inp.substring((inp.indexOf("/") + 1), inp.lastIndexOf("/")), 10)
inpYr = parseInt(inp.substring((inp.lastIndexOf("/") + 1), inp.length), 10)
if (isNaN(inpMo) || isNaN(inpDay) || isNaN(inpYr)) {
msg = "There is some problem with your date entry."
}
if (isNaN(testMo) || isNaN(testDay) || isNaN(testYr)) {
msg = "Couldn't convert your entry to a valid date. Try again."
}
if (testMo != inpMo || testDay != inpDay || testYr != inpYr) {
msg = "Check the range of your date value."
}
if (msg) {
alert(msg)
setTimeout("doSelection(document.forms['" +
fld.form.name + "'].elements['" + fld.name + "'])", 0)
return false
} else {
alert("Data OK");
return true
}
}
function doSelection(fld) {
fld.focus()
fld.select()
}
HTML
<form name="entryForm" onSubmit="return false">
Entre com a data (dd/mm/yyyy): <input type="text" name="startDate" onChange="validarData(this)">
</form>
I tried to make some inversions in testMo with testDay, inpMo with inpDay but without success.