2012-12-11 39 views
0

IAM工作如下:(IAM不是太瞭解jQuery的大部分)..驗證問題與一個jQuery驗證JQuery的

我無法辦到的事情如下:

  1. 只有字母a到z(小寫), 「 - 」(短劃線或連字符)和 「」 (空間)是允許的,
  2. 的 「 - 」(短劃線)以及 「」 必須輸入(空間)的字母,
  3. 「 - 」或「」字母不能是第一或最後 字母輸入,
  4. 「 - 」不能是直接相鄰或相近(之前或之後 )到「」,
  5. 「 - 」或「」一定不能直接相鄰(鄰近) 本身。
  6. 的 '電話' 號碼字段(4位區號,一個空格,7位 本地代碼)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
    <script type="text/javascript" 
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> 
    </script> 

    <script type="text/javascript" 
      src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"> 
    </script> 

    <script> 
    $(document).ready(function(){ 
     $("#commentForm").validate({ 
     onfocusout: function(element) { $(element).valid(); } , 
     rules: { 
      fullname : { 
       required: true, 
       maxlength: 14, 
      },     
      email: { 
       required: true, 
       email: true 
      } 
     }, 
     messages: { 
      fullname : { 
       required: "Please specify your Full Name", 
       maxlength: "Please enter only upto 14 characters", 
      }, 
      email: { 
       required: "We need your email address to contact you", 
       email: "Your email address must be in the format of [email protected]" 
      } 
     } 
     }); 
    }); 

    </script> 

    </head> 

    <body> 

    <form id="commentForm" method="get" action=""> 

     <fieldset> 

     <p> 
     <label for="fullname">Full Name</label> 
     <em>*</em><input id="fullname" name="fullname" size="25" class="required" maxlength="14" /> 
     </p> 


     <p> 
     <label for="email">E-Mail</label> 
     <em>*</em><input id="email" name="email" size="25" class="required email" /> 
     </p> 

    </fieldset> 

    </form> 
    </body> 
</html> 

請別人幫我實現這個..?

+0

檢查[Documentation](http://docs.jquery.com/Plugins/Validation#Options_for_the_validate.28.29_method)。看起來你試圖達到的目標不是這個圖書館的一部分。 – SeinopSys

+0

@ DJDavid98僅在該文檔的幫助下,我已經編寫了上述代碼..但是在閱讀該文檔之後,我不知道如何爲其他幾件事做驗證......順便說一句,請您告訴我在何處添加此方法?從這裏:http://docs.jquery.com/Plugins/Validation/Validator/addMethod – Clarsen

+1

如果你只驗證兩個領域,我建議不使用jQuery驗證。您可以輕鬆編寫自己的代碼,以使用正則表達式驗證這兩個字段。它很簡單,並讓您更多地瞭解驗證如何工作。對不起,沒有回答你的問題。 – srijan

回答

0

使用正則表達式!

  • 僅字母A到Z(小寫), 「 - 」(短劃線或連字符)和 「」(空格)被允許

    !/[^a-z0-9 -]/.test(input)

  • 的 「 - 」(短劃線)和「」(空格)字母必須輸入,

    / /.test(input) && /-/.test(input)

  • 的「 - 」或「」盤符不能是第一或進入最後一個字母,

    !/^[ |-]|[ |-]$/.test(input)

  • 「 - 」不能是直接相鄰或相近(之前或之後)到「」,

  • 「 - 」或「」一定不能直接鄰居(鄰近)。

    !/ -|- |--| /.test(input)

  • 的 '電話' 號碼字段(4位地區碼,空間,7位本地代碼)

    /^\d{4} \d{7}$/.test('1234 1234567')

這個表達式中的每一個返回,如果真條件得到滿足,否則爲假,如下所示:

var input = $('some-selector').value(); 
if(
    !/[^a-z0-9 -]/.test(input) && 
//.test(input) && /-/.test(input) && 
    !/^[ |-]|[ |-]$/.test(input) && 
    !/ -|- |--| /.test(input) && 
    /^\d{4} \d{7}$/.test(input) 
){ 
    //do something 
} 

更重要的是,如果格式化的電話號碼,刪除所有非數字字符,

input = input.replace(/[^0-9]/,'') 

指望他們(它們必須是11)

input.length == 11 

並根據需要再進行格式化。

+0

請使用勾號標記(')來格式化您的內聯代碼。 – Sparky

+0

@Xocatzin:感謝您的代碼,請問您能否告訴我如何在字段之外顯示錯誤消息,而不是彈出格式。 – Clarsen

+0

讓我編輯答案 – Xocoatzin