2014-02-09 43 views
0

我有一個登記表,在這種形式我強制用戶選擇一個國家:如何強制正則表達式驗證所選的<option>標記?

HTML

<label class="required" for="country" title="Select your country">Country 
     <select id="country" name="country" style="width:300px;"> 
     <option id="Russia" value="Russia" >Russia</option> 
     <option id="Iran" value="Iran" >Iran</option> 
     <option id="UnitedStates" value="United States" >United States</option> 
     <option id="China" value="China" >China</option> 

      <option id="UnitedKingdom" value="United Kingdom" >United Kingdom</option> 
      <option id="Australia" value="Australia" >Australia</option> 
      <option id="Canada" value="Canada" >Canada</option> 
      <option id="France" value="France" >France</option> 
      <option id="New Zealand" value="New Zealand" >New Zealand</option> 
      <option id="Brazil" value="Brazil" >Brazil</option> 
      <option id="" value="----" selected="selected">----</option> 
      <option id="Afghanistan" value="Afghanistan" >Afghanistan</option> 
      <option id="Albania" value="Albania" >Albania</option> 
      <option id="Argentina" value="Argentina" >Argentina</option> 
      <option id="Armenia" value="Armenia" >Armenia</option> 
      <option id="Austria" value="Austria" >Austria</option> 
      <option id="Azerbaijan" value="Azerbaijan" >Azerbaijan</option> 
      <option id="Bahamas" value="Bahamas" >Bahamas</option> 
      <option id="Bangladesh" value="Bangladesh" >Bangladesh</option> 
      <option id=Belarus"" value="Belarus" >Belarus</option> 
      <option id="Belgium" value="Belgium" >Belgium</option> 
      <option id="Bulgaria" value="Bulgaria" >Bulgaria</option> 
      <option id="Cambodia" value="Cambodia" >Cambodia</option> 
      <option id="AfricanRepublic" value="Central African Republic" >Central African Republic</option> 
      <option id="Chile" value="Chile" >Chile</option> 
      <option id="China" value="China" >China</option> 
      <option id="Croatia" value="Croatia" >Croatia</option> 
      <option id="Cyprus" value="Cyprus" >Cyprus</option> 
      <option id="Denmark" value="Denmark" >Denmark</option> 
      <option id="Ecuador" value="Ecuador" >Ecuador</option> 
      <option id="Egypt" value="Egypt" >Egypt</option> 
      <option id="Finland" value="Finland" >Finland</option> 
      <option id="Georgia" value="Georgia" >Georgia</option> 
      <option id="Germany" value="Germany" >Germany</option> 
      <option id="Greece" value="Greece" >Greece</option> 
      <option id="Guyana" value="Guyana" >Guyana</option> 
      <option id="HK" value="Hong Kong" >Hong Kong</option> 
      <option id="Indonesia" value="Indonesia" >Indonesia</option> 
      <option id="Iran" value="Iran" >Iran</option> 
      <option id="Iraq" value="Iraq" >Iraq</option> 
      <option id="Israel" value="Israel" >Israel</option> 
      <option id="Italy" value="Italy" >Italy</option> 
      <option id="Kazakhstan" value="Kazakhstan" >Kazakhstan</option> 
      <option id="Kyrgyzstan" value="Kyrgyzstan" >Kyrgyzstan</option> 
      <option id="Malaysia" value="Malaysia" >Malaysia</option> 
      <option id="Malaysia" value="Maldives" >Maldives</option> 
      <option id="Mauritania"` value="Mauritania" >Mauritania</option> 
      <option id="Netherlands" value="Netherlands" >Netherlands</option> 
      <option id="Pakistan" value="Pakistan" >Pakistan</option> 
      <option id="Russia" value="Russia" >Russia</option> 
      <option id="Moldova" value="Moldova" >Moldova</option> 
      <option id="Spain" value="Spain" >Spain</option> 
      <option id="Tajikistan" value="Tajikistan" >Tajikistan</option> 
      <option id="Turkey" value="Turkey" >Turkey</option> 
      <option id="Turkmenistan" value="Turkmenistan" >Turkmenistan</option> 
      <option id="Ukraine" value="Ukraine" >Ukraine</option> 
      <option id="Uzbekistan" value="Uzbekistan" >Uzbekistan</option> 
     </select> 
     </label> 

在我的jQuery我驗證用戶選擇如下

jQuery

// Extend jQuery object to include Regular expression masks assigned to properties 
    mask = jQuery.extend({selectdmask:/^(?:[^S]+|S(?!elected))*$/,textfieldmask: /^[a-z\.\s-]{5,}$/i,phonemask: /^[0-9\(\)\+\.\s-]{8,}$/i,passwordmask: /^\w{5,}$/, emailmask:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/}); 
    // Extend jQuery object to include error messages assigned to properties 
errmsg = jQuery.extend({selecterr:"Select your country",textfielderr:"5 or more letters",phoneerr: "Include dialling code",passworderr:"Minimum 5 characters",emailerr:"Invalid address",matcherr: "Must match"}); 

    // Set up variables to hold details of which mask to use and whether the field should match another field 
var masktouse = null; 
    var mustmatch = null; 
    // Determine the type of mask we're going to validate against 
switch(obj.name) { 
    case "name":   masktouse="textfieldmask";  errtouse="textfielderr"; break; 
    case "phone":  masktouse="phonemask";   errtouse="phoneerr";  break; 
    case "username":  masktouse="textfieldmask";  errtouse="textfielderr"; break; 
     case "email":  masktouse="emailmask";   errtouse="emailerr";  break; 
    case "password":  masktouse="passwordmask";  errtouse="passworderr";  mustmatch="verpassword"; break; 
    case "verpassword": masktouse="passwordmask";  errtouse="passworderr";  mustmatch="password";  break; 
    case "country": masktouse="selectdmask";   errtouse="selecterr"; break; 
    } 

我需要的批准,如果用戶選擇任何國家,但不是這個:

<option id="" value="----" selected="selected">----</option> 

眼下selectdmask:不驗證,如果用戶選擇一個國家或價值=「---- 」。

請幫我修改selectdmask:/^(?:[^S]+|S(?!elected))*$/以批准用戶選擇,只有當用戶選擇國家但不是。

我已經試過

/^[-]{2} Select [-]{2}$/ 

/^(!?--\s*[Ss]elect\s*--)$/ 

但仍沒有運氣。

請不要提示我不同的方式,比如使用jquery來檢查dom等等,只有一個函數來驗證整個表單的元素,我不想編寫一個單獨的查詢來驗證國家,所以請只幫我修改我的正則表達式。

+0

@AmitJoki我看不到答案,但我找出答案^(!!---- $)。*在這裏工作正常。謝謝您的幫助。 –

回答

2

檢查是否option.value !== "----",這應該是足夠的。 你肯定不需要這個正則表達式。

+0

我需要使用正則表達式作爲整個表單修改正在使用正則表達式。要使用您的建議,我需要編寫一個不同的功能來修改國家/地區字段。不是一個好主意 –

+0

^(?!---- $)。*是答覆感謝哥們答覆我。讚賞 –