在单个数字月份之前添加零,在javascript中添加日期(MM / DD / YYYY)

时间:2016-05-06 13:10:46

标签: javascript

function validateContact() {
       <%-- var summary = "";

        summary+=CheckForEmpty('<%=txtName.ClientID%>',"");--%>
        var msg = "";
        var count = 0;

        var contactname = document.getElementById('<%=txtName.ClientID%>').value;
        var alphaExp = /^[A-Za-z0-9 ]{3,20}$/;
        if (contactname.trim().length == 0 || contactname == "" || contactname == null || !contactname.match(alphaExp)) {
            count++;
            msg += count + " Enter First Name \n";
        }

        var lastname = document.getElementById('<%=txtName2.ClientID%>').value;
        var alphaExp = /^[A-Za-z0-9 ]{3,20}$/;
        if (lastname.trim().length == 0 || lastname == "" || lastname == null ) {
            count++;
            msg += count + " Enter Last Name \n";
        }

        var ct_company = document.getElementById('<%=txt_cp_company.ClientID%>').value;
        if (ct_company.length == 0 || ct_company == "" || ct_company == null) {
            count++;
            msg += count + " Enter Company Name \n";
        }

        var designation = document.getElementById('<%=txtDesignation.ClientID%>').value;
    var alphaExp = /^[A-Za-z0-9 ]{2,30}$/;
    if (designation.trim().length == 0 || designation == "" || designation == null ) {
        count++;
        msg += count + " Enter Designation  \n";
    }

    var emailid = document.getElementById('<%=txtEmailId.ClientID%>').value;
        var regxEmailID = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        //var regxEmailID = /^([a-zA-Z0-9_\.\a-zA-Z0-9])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (emailid.trim().length == 0 || emailid == "" || emailid == null) {
        count++;
        msg += count + " Enter Email-Id  \n";
    }
    else if (!emailid.match(regxEmailID)) {
        count++;
        msg += count + "Enter Valid Email-Id  \n";
    }

    var address = document.getElementById('<%=txt_cp_address.ClientID%>').value;
    var alphaExp = /^[A-Za-z0-9 ]{3,20}$/;
    if (address.trim().length == 0 || address == "" || address == null ) {
        count++;
        msg += count + " Enter Address \n";
    }



    var mobno = document.getElementById('<%=txtMobile.ClientID %>').value;
      var MobExp = /^([0-9])+$/;
      if (mobno == "" || mobno.length != 10) {
          count++;
          msg += count + " Enter Mobile No  \n";
      }
      else if (!mobno.match(MobExp)) {
          count++;
          msg += count + "Enter Valid Mobile No \n";

      }

      var dob = document.getElementById('<%=txt_dob.ClientID%>').value;
      var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
        if (dob =="MM/DD/YYYY")
        {
            document.getElementById('<%=txt_dob.ClientID%>').value = "";
        }
        else if (!dob.match(re)) {
            count++;
            msg += count + " InValid date of birth \n";
        }
        else {
            var datestring = dob.replace(new RegExp("/", "g"), '');
            if (datestring.substring(0, 2) < 1 || datestring.substring(0, 2) > 12) {
                count++;
                msg += count + " You've entered incorrect month \n";
            }
            if (datestring.substring(2, 4) < 1 || datestring.substring(2, 4) > 31) {
                count++;
                msg += count + " You've entered incorrect date \n";
            }
        }


      if (document.getElementById('<%=rbtn_yes.ClientID%>').checked == false && document.getElementById('<%=rbtn_no.ClientID%>').checked == false) {
          count++;
          msg += count + " Enter Decision Maker \n";

      }           

      if (msg.toString() == "") {
      }
      else {
          alert(msg);

          return false;
      }


  }

如果月份或日期是单个数字,我想填零。此代码不接受单个数字的月份和日期,如果我输入2015年8月8日,它应该看起来像2015年8月8日。

1 个答案:

答案 0 :(得分:0)

试试这段代码:

summary+=CheckForEmpty('<%=txtName.ClientID%>',"");--%>
var msg = "";
var count = 0;

var contactname = document.getElementById('<%=txtName.ClientID%>').value;
var alphaExp = /^[A-Za-z0-9 ]{3,20}$/;
if (contactname.trim().length == 0 || contactname == "" || contactname == null || !contactname.match(alphaExp)) {
    count++;
    msg += count + " Enter First Name \n";
}

var lastname = document.getElementById('<%=txtName2.ClientID%>').value;
var alphaExp = /^[A-Za-z0-9 ]{3,20}$/;
if (lastname.trim().length == 0 || lastname == "" || lastname == null ) {
    count++;
    msg += count + " Enter Last Name \n";
}

var ct_company = document.getElementById('<%=txt_cp_company.ClientID%>').value;
if (ct_company.length == 0 || ct_company == "" || ct_company == null) {
    count++;
    msg += count + " Enter Company Name \n";
}

var designation = document.getElementById('<%=txtDesignation.ClientID%>').value;
var alphaExp = /^[A-Za-z0-9 ]{2,30}$/;
if (designation.trim().length == 0 || designation == "" || designation == null ) {
    count++;
    msg += count + " Enter Designation  \n";
}

var emailid = document.getElementById('<%=txtEmailId.ClientID%>').value;
var regxEmailID = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
//var regxEmailID = /^([a-zA-Z0-9_\.\a-zA-Z0-9])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (emailid.trim().length == 0 || emailid == "" || emailid == null) {
    count++;
    msg += count + " Enter Email-Id  \n";
}
else if (!emailid.match(regxEmailID)) {
    count++;
    msg += count + "Enter Valid Email-Id  \n";
}

var address = document.getElementById('<%=txt_cp_address.ClientID%>').value;
var alphaExp = /^[A-Za-z0-9 ]{3,20}$/;
if (address.trim().length == 0 || address == "" || address == null ) {
    count++;
    msg += count + " Enter Address \n";
}



var mobno = document.getElementById('<%=txtMobile.ClientID %>').value;
var MobExp = /^([0-9])+$/;
if (mobno == "" || mobno.length != 10) {
    count++;
    msg += count + " Enter Mobile No  \n";
}
else if (!mobno.match(MobExp)) {
    count++;
    msg += count + "Enter Valid Mobile No \n";

}

var dob = document.getElementById('<%=txt_dob.ClientID%>').value;
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

if (dob =="MM/DD/YYYY")
{
    document.getElementById('<%=txt_dob.ClientID%>').value = "";
}
else if (!dob.match(re)) {
    count++;
    msg += count + " InValid date of birth \n";
}

else {
    var datestring = dob.replace(new RegExp("/", "g"), '');
    // get month and day
    var month = datestring.substring(0, 2);
    var day = datestring.substring(2, 4);
    // check
    if(month<10){
        month = "0"+ month;
        // replace
        dob.replace(dob.substring(0, 2), month);
    }
    if(day<10){
        day = "0"+ day;
        dob.replace(dob.substring(2, 4), day);
    }


    if (datestring.substring(0, 2) < 1 || datestring.substring(0, 2) > 12) {
        count++;
        msg += count + " You've entered incorrect month \n";
    }
    if (datestring.substring(2, 4) < 1 || datestring.substring(2, 4) > 31) {
        count++;
        msg += count + " You've entered incorrect date \n";
    }
}


if (document.getElementById('<%=rbtn_yes.ClientID%>').checked == false && document.getElementById('<%=rbtn_no.ClientID%>').checked == false) {
    count++;
    msg += count + " Enter Decision Maker \n";

}

if (msg.toString() == "") {
}
else {
    alert(msg);

    return false;
}


}