gpt4 book ai didi

javascript - 如何强制正则表达式验证所选的

转载 作者:行者123 更新时间:2023-12-02 17:46:55 24 4
gpt4 key购买 nike

我有一个注册表单,在该表单中我强制用户选择一个国家/地区:

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 等,只有一个函数来验证整个表单的元素,我不想编写一个单独的查询来验证国家/地区,所以请只帮助我修改我的正则表达式。

最佳答案

检查 option.value !== "----" 是否足够。您绝对不需要正则表达式。

关于javascript - 如何强制正则表达式验证所选的 <option> 标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21657792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com