Ras
Ras

Reputation: 628

Codeigniter dropdown validation

This is my code:

        <div class="two-columns">
            <div class="col-half">
                <label for="city">Città</label>
                <input type="text" name="city" class="required">
            </div>
            <div class="col-half">
                <label for="province">Provincia</label><br>
                <select name="province" class="required">
                    <option value="">Seleziona la provincia</option>
                </select>
            </div>
        </div>

As you can see I put the "class required" in the dropdown because the user must choose a value. All works fine but I cannot see the alert message if I miss to select an item from the dropdown; the form is not sent, correct, but the alert is not shown. Any hints?

Upvotes: 0

Views: 1591

Answers (2)

Krishna38
Krishna38

Reputation: 735

If you add like this class='required' it will not do any kind of validation.

  • The class attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript (via the HTML DOM) to make changes to HTML elements with a specified class.

If you want to do validation you can do it in different ways.

  • By Using HTML5 required attribute

    For Ex : <select name="" required>

  • Server side form validation in codeigniter. Dont forgot to include validation libraray $this->load->library('form_validation');

    For Ex : $this->form_validation->set_rules("province","province","required")

  • Finally you can use jquery or javascript validation

    Hope it will help. Thanks

Upvotes: 1

Jeremy Jackson
Jeremy Jackson

Reputation: 2257

You should be able to just have it as

<select name="province" required>
    <option value="">Seleziona la provincia</option>
</select>

Here's a link to a helpful stackoverflow post about it:

Can I apply the required attribute to <select> fields in HTML5?

Upvotes: 1

Related Questions