我有一个注册表单,在此表单中我强制用户选择一个国家/地区:
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 :(得分:2)
检查option.value !== "----"
是否足够。
你绝对不需要正则表达式。