如何在页面验证失败时阻止提交页面

时间:2015-11-30 08:47:28

标签: javascript

我写了日期验证代码,但是当日期无效时,它会发出警报但不会阻止它提交。

从表单标签我调用方法validateDate1,我也使用了关键字返回静止页面,当我点击保存ch

<form method="post" onsubmit="return validateDate1(<%=request.getParameter("i_effectiveDates") %>) " action="<%=response.encodeRedirectURL("productDetail.jsp?ec=" + productEntityCode)%>" accept-charset="UTF-8">

function validateDate1(input)
{
    var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
    var returnval=false
    if (!validformat.test(input.value))
        alert("Invalid Date Format. Please correct and submit again.")
    else
    { //Detailed check for valid date ranges
        var monthfield=input.value.split("/")[0]
        var dayfield=input.value.split("/")[1]
        var yearfield=input.value.split("/")[2]
        var dayobj = new Date(yearfield, monthfield-1, dayfield)
        if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
            alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
        else
            returnval=true
    }

    if (returnval==false)       
    input.select()
    return returnval
}

0 个答案:

没有答案