Change the date validation of the mm / dd / yyyy format script to the format dd / mm / yyyy

2

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.

    
asked by anonymous 15.05.2017 / 01:09

1 answer

1

The only thing I did was to format fld.value , I left it in the format that it understands, mm / dd / yyyy, so I gave a var testDate = new Date(inp) with inp formatted, so it validated the date the way right, then just do the validation normally.

function validarData(fld) {
  var testMo, testDay, testYr, inpMo, inpDay, inpYr, msg
  var inp = fld.value

  inp = parseInt(inp.substring((inp.indexOf("/") + 1), inp.lastIndexOf("/")), 10) + "/" + parseInt(inp.substring(0, inp.indexOf("/")), 10) + "/" + parseInt(inp.substring((inp.lastIndexOf("/") + 1), inp.length), 10)
  console.log(inp);
  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()
}
<form name="entryForm" onSubmit="return false">
  Entre com a data (dd/mm/yyyy): <input type="text" name="startDate" onChange="validarData(this)">
</form>
    
15.05.2017 / 01:57