Change script date validation from mm/dd/yyyy format to dd/mm/yyyy format

Asked

Viewed 228 times

2

I have a script that validates date in mm/dd/yyyy format but 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 do some inversions in testMo with testDay, inpMo with inpDay but without success.

1 answer

0


All I did was format the fld.value, left in the format he understands, mm/dd/yyyy, so I gave a var testDate = new Date(inp) with the inp formatted, so it valid the date the right way, 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.