Kissa Mia Kissa Mia - 2 months ago 11
HTML Question

How to force regex to validate selected <option> tag?

I have a registration form, in this form I force the users to select a country:


<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>

In my jQuery I validate the user selection as follow


// 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( {
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;

What I need is to approve, if user selects any countries but not this:

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

Right now selectdmask: won't validate, if the user selected a country or value="----".

Please help me to modify the
to approve the user selection only if the user selected a country but not

I have tried

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



but still no luck.

Please don't suggest me different ways, like to use jquery to check the dom and so on, there is only one function to validate the whole form's elements, I don't want to write one separated query to validate the country, so please only help me to modify my regex.


Check if option.value !== "----", that should be enough. You definitely don't need Regex for this.