2017-06-26 45 views
-1

IM使用JavaScript功能簡單的客戶端驗證如何驗證在JavaScript

`function IsCodeEmpty() { 
      if (document.getElementById('InputCode').value == "") { 
       return 'Patient Code should not be empty'; 
      } 
      else { return ""; } 
     } 

function IsVisitNoInValid() { 
    if (document.getElementById('VisitNo').value== "") { 
     return 'Visit No should not be empty'; 
    } 

    else { return ""; } 
} 

function IsValid() { 

    var CodeValidationMessage = IsCodeEmpty(); 
    var IsVisitNoInValidMessage = IsVisitNoInValid(); 
    var FinalErrorMessage = "Errors:"; 
    if (PatientCodeValidationMessage != "") 
     FinalErrorMessage += "\n" + CodeValidationMessage; 
    if (IsVisitNoInValidMessage != "" 
     FinalErrorMessage += "\n" + IsVisitNoInValidMessage; 

    if (FinalErrorMessage != "Errors:") { 
     alert(FinalErrorMessage); 
     return false; 
    } 
    else { 
     return true; 
    } 
}` 

後的操作方法和我的看法是`

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>AddPatient</title> 
    <link href="@Url.Content("~/Content/add.css")" rel="stylesheet" type="text/css" /> 
    <script src="~/Scripts/Validation.js"></script> 
</head> 
<body> 
    <div class="form-group"> 
     <form action="~/Main/Screen" method="post"> 
      <div class="panel panel-primary"> 
       <div class="panel-heading">TEST ORDER FOR PATIENT</div> 
       <div class="panel-body"> 

        <h3><label class="control-label"> CODE<span class="inp">*</span></label></h3><input type="text" id="InputCode" name="InputCode" value="" style="height:30px;" /><br /> 

        <h3><label class="control-label">VISIT NO<span class="inp">*</span></label></h3><input type="text" id="visitno" name="VisitNo" value="" style="height:30px;" /><br /> 

        <br /> 
        <button class="btn btn-primary" id="add" type="submit" onclick="return IsValid()";>ADD</button> 
       </div> 
      </div> 
      </form>` 

,爲什麼我不能讓我的驗證消息。它不顯示任何驗證消息。 我在頭部添加了腳本鏈接。那麼爲什麼我沒有得到運行javascript

回答

0
  1. 第4 if條件語句的右括號是錯過了。
  2. visitno不等於VisitNo。您需要在HTML中替換visitno標識符與VisitNo,或者用visitno替換爲VisitNo

請參閱下面的工作代碼。

function IsCodeEmpty() { 
 
    if (document.getElementById('InputCode').value == "") { 
 
    return 'Patient Code should not be empty'; 
 
    } else { 
 
    return ""; 
 
    } 
 
} 
 

 
function IsVisitNoInValid() { 
 
    if (document.getElementById('VisitNo').value == "") { 
 
    return 'Visit No should not be empty'; 
 
    } else { 
 
    return ""; 
 
    } 
 
} 
 

 
function IsValid() { 
 
    var CodeValidationMessage = IsCodeEmpty(); 
 
    var IsVisitNoInValidMessage = IsVisitNoInValid(); 
 
    var FinalErrorMessage = "Errors:"; 
 
    if (CodeValidationMessage != "") FinalErrorMessage += "\n" + CodeValidationMessage; 
 
    if (IsVisitNoInValidMessage != "") FinalErrorMessage += "\n" + IsVisitNoInValidMessage; 
 
    if (FinalErrorMessage != "Errors:") { 
 
    alert(FinalErrorMessage); 
 
    return false; 
 
    } else { 
 
    return true; 
 
    } 
 
}
<form action="#" method="post"> 
 
    <div class="panel panel-primary"> 
 
    <div class="panel-heading">TEST ORDER FOR PATIENT</div> 
 
    <div class="panel-body"> 
 

 
     <h3><label class="control-label"> CODE<span class="inp">*</span></label></h3><input type="text" id="InputCode" name="InputCode" value="" style="height:30px;" /><br /> 
 

 
     <h3><label class="control-label">VISIT NO<span class="inp">*</span></label></h3><input type="text" id="VisitNo" name="VisitNo" value="" style="height:30px;" /><br /> 
 

 
     <br /> 
 
     <button class="btn btn-primary" id="add" type="submit" onclick="return IsValid()" ;>ADD</button> 
 
    </div> 
 
    </div> 
 
</form>

+0

thanku其工作。我只是不知道這個ID,並且必須和控制中的一樣 –