2017-02-12 72 views
0

我是新來的HTML/PHP/JavaScript和引導框架...驗證使用錯誤引導按鈕

這裏是我的代碼:

<form name="Enquiry" method="post" action="query.php"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 
<h5>Choose search type:</h5> 
<div id="selector" class="btn-group"> 
    <button type="button" name="Surname" class="btn btn-default active">Surname</button> 
    <button type="button" name="IDNumber" class="btn btn-default">ID Number</button> 
    <button type="button" name="FileNumber" class="btn btn-default">File Number</button> 
</div> 
<br> 
<input id="searchbox" name="QD" type="text" size="20" maxlength="30" pattern="/[A-z]/g" title="0-Only alphabets allowed! No special characters or numbers. Please correct!"> 
<script> 
$('#selector button').click(function() { 
    $(this).addClass('active').siblings().removeClass('active'); 
    $getName = this.name; 

     switch ($getName) { 
      case "Surname": 
      document.getElementById('searchbox').value=""; 
      document.getElementById('searchbox').maxLength="30"; 
      document.getElementById('searchbox').pattern="/[A-z]/g"; 
      document.getElementById('searchbox').title="1-Only alphabets and spaces allowed. Please correct!";   
     break; 
      case "IDNumber": 
      document.getElementById('searchbox').value=""; 
      document.getElementById('searchbox').maxLength="13"; 
      document.getElementById('searchbox').pattern="\d+"; 
      document.getElementById('searchbox').title="2-Only numerical value allowed in the ID field! Please correct";    
     break; 
      case "FileNumber": 
      document.getElementById('searchbox').value=""; 
      document.getElementById('searchbox').maxLength="5"; 
      document.getElementById('searchbox').pattern="\d+"; 
      document.getElementById('searchbox').title="3-Only numerical value allowed in the file number field! Please correct";   
     } 
}); 
</script> 

<input type="submit" class="btn btn-primary" name="search" value="Query"> 
</form> 

的完整代碼在: https://jsfiddle.net/o5n5ucm6/7/

我正在嘗試實現的功能: 根據用戶按下的按鈕,我使用regxp和maxlength等設置輸入文本框的驗證標準。

問題Im具有: 當我單擊查詢按鈕(POST方法)時,即使值與regxp匹配,它仍然會引發錯誤。

任何幫助和建議?

回答

1

Javascript允許使用正則表達式,所以你不需要引用你的行。

改變這一行: document.getElementById('searchbox').pattern="/[A-z]/g";

這樣: document.getElementById('searchbox').pattern=\[A-z]\g;

兩行本: document.getElementById('searchbox').pattern="\d+";

需要改變這樣: document.getElementById('searchbox').pattern=\d+;

+0

不工作 - 拋出客戶端的語法錯誤。 –

+0

它在哪裏拋出語法錯誤?當我使用它時,它適用於我這方面。 –