0

I use jQuery validation plugin from here. Is there a way to check if the field is NOT equal to some string? I have a select element looking like this:

<select id="school_admin" name="school">
   <option value="selector">Select...</option>
   <option value="UDSM">University of Dar Es Salaam</option>
   <option value="ARU">Ardhi University</option>
   <option value="IFM">Institute of Finance Management</option>
</select>

As you can see, first option is 'Select...' and I need to check on submit that it's not set to this first item, but to some another actual option. How can I do that quickly?

5 Answers 5

1

A better way of coding your select might be to use an optgroup:

<select id="school_admin" name="school">
    <optgroup label="Select a school...">
        <option value="selector">Select...</option>
        <option value="UDSM">University of Dar Es Salaam</option>
        <option value="ARU">Ardhi University</option>
        <option value="IFM">Institute of Finance Management</option>
    </optgroup>
</select>

The 'select' label is then not selectable.

http://reference.sitepoint.com/html/optgroup

Sign up to request clarification or add additional context in comments.

1 Comment

I think it's a good solution and I undoubtedly will use it in another application but I need to make that first item visible, not the first item in schools list. Thanks!
0

Why not just change the default option value to blank and leverage the metadata support by declaring a class of required on the select. Here is the demo

Comments

0
if($("#item").html() != "string" ) 

Comments

0

I suggest you leave Select...'s value empty value="" then simply

if ( ( sel = $('#school_admin').val() ).length ) { ... }
else { .. }

Never used the actual plugin, so you should be able to make it work with its own api

Comments

0

Check the value of the select element, and see if it matches the first one.

if ($('#school_admin').val() != 'selector') {
    //Doesn't equal the first one, do your thing
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.