Javascript必填字段验证

时间:2015-01-05 22:03:27

标签: javascript c# sql

我有一个很大的形式,我必须验证44个字段。我使用javascript表单验证来要求字段输入,并使用C#正则表达式来确保有效输入。我不会详细说明为什么我必须这样做,但这是如何运作的。我已经对此代码块进行了三重检查,我必须验证表单并且无法找到错误。代码低于(大),希望有人比我更好的眼睛。

C#

 @using (Html.BeginForm("Create", "Table1_1", FormMethod.Post, new { onsubmit = "return validateit()", name = "myForm" }))
{

的Javascript

 <script>
          function validateit() {
              var x1 = document.forms["myForm"]["COMPANY1"].value;
              var x2 = document.forms["myForm"]["STATE1"].value;
              var x3 = document.forms["myForm"]["BILL_CODE1"].value;
              var x4 = document.forms["myForm"]["EFFECTIVE_DATE1"].value;
              var x5 = document.forms["myForm"]["RECORD_TYPE1"].value;
              var x6 = document.forms["myForm"]["MASK_ID1"].value;
              var x7 = document.forms["myForm"]["EXTENSION_ID1"].value;
              var x8 = document.forms["myForm"]["OVERPAYMENT_LIMIT"].value;
              var x9 = document.forms["myForm"]["UNDERPAYMENT_LIMIT"].value;
              var x10 = document.forms["myForm"]["OVERPAYMENT_REFUND"].value;
              var x11 = document.forms["myForm"]["PARTIAL_PAYMENT_REFUND"].value;
              var x12 = document.forms["myForm"]["RETURN_PREMIUM_REFUND"].value;
              var x13 = document.forms["myForm"]["CANCELLATION_REFUND"].value;
              var x14 = document.forms["myForm"]["EFT_AUTH_CHECK"].value;
              var x15 = document.forms["myForm"]["EFT_CHECK_BILL"].value;
              var x16 = document.forms["myForm"]["EFT_NSF_LIMIT"].value;
              var x17 = document.forms["myForm"]["DB_PREM_ROUND"].value;
              var x18 = document.forms["myForm"]["CC_OPTION"].value;
              var x19 = document.forms["myForm"]["NSF_CHECK_LIMIT"].value;
              var x20 = document.forms["myForm"]["NSF_CHECK_OPTION"].value;
              var x21 = document.forms["myForm"]["FIRST_TERM_TYPE"].value;
              var x22 = document.forms["myForm"]["CARRY_DATE_OPTION"].value;
              var x23 = document.forms["myForm"]["DB_DPAY_ENDORSE_DAYS"].value;
              var x24 = document.forms["myForm"]["DATE_METHOD"].value;
              var x25 = document.forms["myForm"]["RENEWAL_OPTION"].value;
              var x26 = document.forms["myForm"]["DROP_DAYS"].value;
              var x27 = document.forms["myForm"]["MULTI_PAY_IND"].value;
              var x28 = document.forms["myForm"]["MINIMUM_PAYMENT"].value;
              var x29 = document.forms["myForm"]["ENDORSEMENT_ACTION"].value;
              var x30 = document.forms["myForm"]["I_AND_S_OPTION_DAYS"].value;
              var x31 = document.forms["myForm"]["S_OPTION_PERCENT"].value;
              var x32 = document.forms["myForm"]["SERVICE_CHARGE_OPTION"].value;
              var x33 = document.forms["myForm"]["REINSTATE_OPTION"].value;
              var x34 = document.forms["myForm"]["CASH_WITH_APP_OPTION"].value;
              var x35 = document.forms["myForm"]["DB_CC_NOTICE"].value;
              var x36 = document.forms["myForm"]["DOWN_PAY_RENEWAL_DAYS"].value;
              var x37 = document.forms["myForm"]["MONTH_BY_TERM"].value;
              var x38 = document.forms["myForm"]["LEAD_MONTHS"].value;
              var x39 = document.forms["myForm"]["INITIAL_MONTHS"].value;
              var x40 = document.forms["myForm"]["DB_CC_REJECTS"].value;
              var x41 = document.forms["myForm"]["RETURN_ENDORSEMENT_OPTION"].value;
              var x42 = document.forms["myForm"]["RTN_S_OPTION_PERCENT"].value;
              var x43 = document.forms["myForm"]["DB_AUTO_REF_DAY"].value;
              var x44 = document.forms["myForm"]["RENEW_OPTION_BILL_PLAN"].value;

              var count = 0;
              if (x1 == null || x1 == "" || x1 == "    ") {
                  document.getElementById("demo1").innerHTML = "<font style='color:red';>Error: Company number is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo1").innerHTML = null;
              }
              if (x2 == null || x2 == "" || x2 == "    ") {
                  document.getElementById("demo2").innerHTML = "<font style='color:red';>Error: State code is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo2").innerHTML = null;
              }
              if (x3 == null || x3 == "" || x3 == "    ") {
                  document.getElementById("demo3").innerHTML = "<font style='color:red';>Error: Bill code is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo3").innerHTML = null;
              }
              if (x4 == null || x4 == "" || x4 == "    ") {
                  document.getElementById("demo4").innerHTML = "<font style='color:red';>Error: Effective date is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo4").innerHTML = null;
              }
              if (x5 == null || x5 == "" || x5 == "    ") {
                  document.getElementById("demo5").innerHTML = "<font style='color:red';>Error: Record type is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo5").innerHTML = null;
              }
              if (x6 == null || x6 == "" || x6 == "    ") {
                  document.getElementById("demo6").innerHTML = "<font style='color:red';>Error: Mask id is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo6").innerHTML = null;
              }
              if (x7 == null || x7 == "" || x7 == "    ") {
                  document.getElementById("demo7").innerHTML = "<font style='color:red';>Error: Extension id is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo7").innerHTML = null;
              }
              if (x8 == null || x8 == "" || x8 == "    ") {
                  document.getElementById("demo8").innerHTML = "<font style='color:red';>Error: Overpayment limit is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo8").innerHTML = null;
              }
              if (x9 == null || x9 == "" || x9 == "    ") {
                  document.getElementById("demo9").innerHTML = "<font style='color:red';>Error: Underpayment limit is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo9").innerHTML = null;
              }
              if (x10 == null || x10 == "" || x10 == "    ") {
                  document.getElementById("demo10").innerHTML = "<font style='color:red';>Error: Overpayment refund is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo10").innerHTML = null;
              }
              if (x11 == null || x12 == "" || x11 == "    ") {
                  document.getElementById("demo11").innerHTML = "<font style='color:red';>Error: Partial payment refund is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo11").innerHTML = null;
              }
              if (x12 == null || x12 == "" || x12 == "    ") {
                  document.getElementById("demo12").innerHTML = "<font style='color:red';>Error: Return premium refund is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo12").innerHTML = null;
              }
              if (x13 == null || x13 == "" || x13 == "    ") {
                  document.getElementById("demo13").innerHTML = "<font style='color:red';>Error: Cancellation refund is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo13").innerHTML = null;
              }
              if (x14 == null || x14 == "" || x14 == "    ") {
                  document.getElementById("demo14").innerHTML = "<font style='color:red';>Error: EFT auth check</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo14").innerHTML = null;
              }
              if (x15 == null || x15 == "" || x15 == "    ") {
                  document.getElementById("demo15").innerHTML = "<font style='color:red';>Error: EFT check bill is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo15").innerHTML = null;
              }
              if (x16 == null || x16 == "" || x16 == "    ") {
                  document.getElementById("demo16").innerHTML = "<font style='color:red';>Error: EFT NSF limit is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo16").innerHTML = null;
              }
              if (x17 == null || x17 == "" || x17 == "    ") {
                  document.getElementById("demo17").innerHTML = "<font style='color:red';>Error: Premium rounding is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo17").innerHTML = null;
              }
              if (x18 == null || x18 == "" || x18 == "    ") {
                  document.getElementById("demo18").innerHTML = "<font style='color:red';>Error: Credit card option is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo18").innerHTML = null;
              }
              if (x19 == null || x19 == "" || x19 == "    ") {
                  document.getElementById("demo19").innerHTML = "<font style='color:red';>Error: NSF check limit is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo19").innerHTML = null;
              }
              if (x20 == null || x20 == "" || x20 == "    ") {
                  document.getElementById("demo20").innerHTML = "<font style='color:red';>Error: NSF check option is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo20").innerHTML = null;
              }
              if (x21 == null || x21 == "" || x21 == "    ") {
                  document.getElementById("demo21").innerHTML = "<font style='color:red';>Error: First term type is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo21").innerHTML = null;
              }
              if (x22 == null || x22 == "" || x22 == "    ") {
                  document.getElementById("demo22").innerHTML = "<font style='color:red';>Error: Carry date option is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo22").innerHTML = null;
              }
              if (x23 == null || x23 == "" || x23 == "    ") {
                  document.getElementById("demo23").innerHTML = "<font style='color:red';>Error: Down pay endorse days is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo23").innerHTML = null;
              }
              if (x24 == null || x24 == "" || x24 == "    ") {
                  document.getElementById("demo24").innerHTML = "<font style='color:red';>Error: Date method is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo24").innerHTML = null;
              }
              if (x25 == null || x25 == "" || x25 == "    ") {
                  document.getElementById("demo25").innerHTML = "<font style='color:red';>Error: Renewal option is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo25").innerHTML = null;
              }
              if (x26 == null || x26 == "" || x26 == "    ") {
                  document.getElementById("demo26").innerHTML = "<font style='color:red';>Error: Drop days is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo26").innerHTML = null;
              }
              if (x27 = null || x27 == "" || x27 == "    ") {
                  document.getElementById("demo27").innerHTML = "<font style='color:red';>Error: Multipay ind is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo27").innerHTML = null;
              }
              if (x28 == null || x28 == "" || x28 == "    ") {
                  document.getElementById("demo28").innerHTML = "<font style='color:red';>Error: Minimum payment is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo28").innerHTML = null;
              }
              if (x29 == null || x29 == "" || x29 == "    ") {
                  document.getElementById("demo29").innerHTML = "<font style='color:red';>Error: Endorsement action is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo29").innerHTML = null;
              }
              if (x30 == null || x30 == "" || x30 == "    ") {
                  document.getElementById("demo30").innerHTML = "<font style='color:red';>Error: I and S option days is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo30").innerHTML = null;
              }
              if (x31 == null || x31 == "" || x31 == "    ") {
                  document.getElementById("demo31").innerHTML = "<font style='color:red';>Error: S option percent is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo31").innerHTML = null;
              }
              if (x32 == null || x32 == "" || x32 == "    ") {
                  document.getElementById("demo32").innerHTML = "<font style='color:red';>Error: Service charge option is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo32").innerHTML = null;
              }
              if (x33 == null || x33 == "" || x33 == "    ") {
                  document.getElementById("demo33").innerHTML = "<font style='color:red';>Error: Reinstate option is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo33").innerHTML = null;
              }
              if (x34 == null || x34 == "" || x34 == "    ") {
                  document.getElementById("demo34").innerHTML = "<font style='color:red';>Error: Cash with application is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo34").innerHTML = null;
              }
              if (x35 == null || x35 == "" || x35 == "    ") {
                  document.getElementById("demo35").innerHTML = "<font style='color:red';>Error: Credit card notice is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo35").innerHTML = null;
              }
              if (x36 == null || x36 == "" || x36 == "    ") {
                  document.getElementById("demo36").innerHTML = "<font style='color:red';>Error: Down pay renewal days is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo36").innerHTML = null;
              }
              if (x37 == null || x37 == "" || x37 == "    ") {
                  document.getElementById("demo37").innerHTML = "<font style='color:red';>Error: Month by term is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo37").innerHTML = null;
              }
              if (x38 == null || x38 == "" || x38 == "    ") {
                  document.getElementById("demo38").innerHTML = "<font style='color:red';>Error: Lead months is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo38").innerHTML = null;
              }
              if (x39 == null || x39 == "" || x39 == "    ") {
                  document.getElementById("demo39").innerHTML = "<font style='color:red';>Error: Initial months is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo39").innerHTML = null;
              }
              if (x40 == null || x40 == "" || x40 == "    ") {
                  document.getElementById("demo40").innerHTML = "<font style='color:red';>Error: Credit card rejects is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo40").innerHTML = null;
              }
              if (x41 == null || x41 == "" || x41 == "    ") {
                  document.getElementById("demo41").innerHTML = "<font style='color:red';>Error: Return endorsement option is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo41").innerHTML = null;
              }
              if (x42 == null || x42 == "" || x42 == "    ") {
                  document.getElementById("demo42").innerHTML = "<font style='color:red';>Error: Return S option percent is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo42").innerHTML = null;
              }
              if (x43 == null || x43 == "" || x43 == "    ") {
                  document.getElementById("demo43").innerHTML = "<font style='color:red';>Error: Auto ref day is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo43").innerHTML = null;
              }
              if (x44 == null || x44 == "" || x44 == "    ") {
                  document.getElementById("demo44").innerHTML = "<font style='color:red';>Error: Renew option bill plan is required</font>";
                  count = count + 1;
              }
              else {
                  document.getElementById("demo44").innerHTML = null;
              }
              if (count > 0) {
                  return false;
              }
          }
</script>

注意:此代码适用于我的所有其他视图。我必须在某个地方打个错字,但我似乎无法找到它。谢谢!

1 个答案:

答案 0 :(得分:0)

x27检查分配给null而不是测试null:

if (x27 = null || x27 == "" || x27 == "    ") {

应该是

if (x27 == null || x27 == "" || x27 == "    ") {

如果你确实需要这个功能,你可能会发现这样的东西更容易使用:

function validateit() {

    var fields = [
        ["COMPANY1", "Company number is required"],
        ["STATE1", "State code is required"],
        ["BILL_CODE1", "BILL_CODE1 is required"],
        ["EFFECTIVE_DATE1", "EFFECTIVE_DATE1 is required"],
        ["RECORD_TYPE1", "RECORD_TYPE1 is required"],
        // ...
    ];

    var count = 0;
    for (var i = 0; i < fields.length; i++) {
        var x = document.forms.myForm[fields[i][0]].value;
        if (x === null || x === "" || x === "    ") {
            count++;
            document.getElementById("demo" + (i + 1)).innerHTML = "<font style='color:red';>Error: " + fields[i][1] + "</font>";
        }
        else {
            document.getElementById("demo" + (i + 1)).innerHTML = "";
        }
    }

}