如何在.asp中使用Javascript验证多功能

时间:2015-02-12 09:38:31

标签: javascript asp.net validation

我正在尝试验证表单上的6个函数。我正在为每个函数设置适当的警报,但表单似乎没有正确验证,并且在“生成”时提交表单。按下按钮。

我非常感谢任何关于此的建议

这就是我目前设定的方式:

function ValidateFields(){
        //Validate all Required Fields
        if (RequiredTextValidate()&& CheckDateTime()&& ReasonAbsenceValidate()&&
        ReturnDateChanged() && FirstDateChanged() && ActualDateChanged())return true;
        }



  <script type="text/javascript" language="Javascript">

      function RequiredTextValidate() {
             //check all required fields are completed
            if (document.getElementById("<%=CompletedByTextBox.ClientID%>").value == "") {
                alert("Completed by field cannot be blank");
                document.getElementById("<%=CompletedByTextBox.ClientID%>").focus();
                return false;
            }


            if (document.getElementById("<%=CompletedExtTextBox.ClientID %>").value == "") {
                alert("Completed By Extension field cannot be blank");
                document.getElementById("<%=CompletedExtTextBox.ClientID %>").focus();
                return false;
            }

            if (document.getElementById("<%=EmployeeNoTextBox.ClientID %>").value == "") {
                alert("Employee No field cannot be blank");
                document.getElementById("<%=EmployeeNoTextBox.ClientID %>").focus();
                return false;
            }

            if (document.getElementById("<%=EmployeeNameTextBox.ClientID %>").value == "") {
                alert("Employee Name field cannot be blank");
                document.getElementById("<%=EmployeeNameTextBox.ClientID %>").focus();
                return false;
            }

            return true;
        }

     function CheckDateTime() {
            // regular expression to match required date format
            re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

            if (document.getElementById("<%=SickDateTextBox.ClientID %>").value != '' && !document.getElementById("<%=SickDateTextBox.ClientID %>").value.match(re)) {
                alert("Invalid date format: " + document.getElementById("<%=SickDateTextBox.ClientID %>").value);
                document.getElementById("<%=SickDateTextBox.ClientID %>").focus();
                return false;
            }

            // regular expression to match required time format
            re = /^\d{1,2}:\d{2}([ap]m)?$/;

            if (document.getElementById("<%=SickTimeTextBox.ClientID %>").value != '' && !document.getElementById("<%=SickTimeTextBox.ClientID %>").value.match(re)) {
                alert("Invalid time format: " + document.getElementById("<%=SickTimeTextBox.ClientID %>").value);
                document.getElementById("<%=SickTimeTextBox.ClientID %>").focus();
                return false;
            }
           
            return true;
        }

    function ReasonAbsenceValidate() {
    //check that reason for absence field is completed
            if (document.getElementById("<%=ReasonTextBox.ClientID%>").value == "") {
                alert("Reason for absence field cannot be blank");
                document.getElementById("<%=ReasonTextBox.ClientID%>").focus();
                return false;
            }
            return true;
        }

    function ReturnDateChanged() {
     // check that either Return date or Date Unknown fields are completed 
        var ReturnDateValid = document.getElementById("<%=ReturnDateTextBox.ClientID%>").value;
        var ReturnDateUnknown = document.getElementById("<%=ReturnUnknownCheckBox.ClientID%>").checked;
        
        if (ReturnDateUnknown.checked) 
        {
        ReturnDateValid.disabled = false;
        ReturnDateUnknown.disabled = "disabled";
        }

    if (ReturnDateValid == "" && ReturnDateUnknown == "") {
        alert("You must enter at least one field for anticipated return");
        return false;
        }  
        return true;
        }
        
        function FirstDateChanged() {
         // check that either First date of sickness or Date Unknown fields are completed 
         var FirstDateValid = document.getElementById("<%=FirstDateTextBox.ClientID%>").value;
          var FirstDateUnknown = document.getElementById("<%=FirstDateUnknownCheckBox.ClientID%>").checked;
          if (FirstDateUnknown.checked) 
          {
            FirstDateValid.disabled = false;
            FirstDateUnknown.disabled = "disabled";
          
            }
            if (FirstDateValid == "" && FirstDateUnknown == "") {
            alert("You must enter at least one field for first day of sickness");
            return false;
            }
            return true;
            }
       
   
   function ActualDateChanged() {
       // check that either Actual date of return or Date Unknown fields are completed 
       var ActualDateValid = document.getElementById("<%=ActualDateTextBox.ClientID%>").value;
       var ActualDateUnknown = document.getElementById("<%=ActualDateUnknownCheckBox.ClientID%>").checked;

       if (ActualDateUnknown.checked) 
       {
           ActualDateValid.disabled = false;
           ActualDateUnknown.disabled = "disabled";
     
           }
           if (ActualDateValid == "" && ActualDateUnknown == "") {
               alert("You must enter at least one field for actual date of return");
               return false;
           }
           return true;
           }

     function ValidateFields(){
        //Validate all Required Fields
        if (RequiredTextValidate()&& CheckDateTime()&& ReasonAbsenceValidate()&&
        ReturnDateChanged() && FirstDateChanged() && ActualDateChanged())return true;
        }
</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

更新功能如下:

如果输入无效,您需要返回flase以防止发布表单。

function ValidateFields() {
    //Validate all Required Fields
    if (RequiredTextValidate() && CheckDateTime() && ReasonAbsenceValidate() && ReturnDateChanged() && FirstDateChanged() && ActualDateChanged()) {
        return true;
    } else {
        return false;
    }
}

<强>更新

if (document.getElementById("<%=SickTimeTextBox.ClientID %>").value != '' &&
!document.getElementById("<%=SickTimeTextBox.ClientID %>").value.match(re))

条件似乎是错误的。应该如下。

if (document.getElementById("<%=SickTimeTextBox.ClientID %>").value == '' &&
!document.getElementById("<%=SickTimeTextBox.ClientID %>").value.match(re))

使用==代替!=运算符。