0

I want to validate a drop down box. It should show a validation message if the value in the dropdown box is null. I am in a situation of not removing null value from default option.I have the following options for dropdown list.

' '(null value by default)

option1

option2

option3

I am using regular expression to pass null value.

http://jsfiddle.net/fY2xm/

 I don't know is that correct passing null value. or any other way.

2 Answers 2

2

In the given case you need to use required validation because since the regexp rule is testing for optional condition it will always return true since the value is empty for the null option.

$("#formname").validate({
    debug: true, //prevent submission for testing
    rules: {
        textbox: {
            required: true
        }
    }
});

Demo: Fiddle

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

1 Comment

hey it's not working. It consider null as a value. I tried it initially but no use.
1

From your fiddle, your not passing values.

<select name='select'> <!--Don't forget to give name-->
        <option value=''></option>
        <option value='1'>Australia</option>
        <option value='2'>England</option>
        <option value='3'>United States</option>
    </select>

Then JS:

$("#formname").validate({
    debug: true, //prevent submission for testing
    rules: {
        'select': {
            required: true ---------------------
        }                                      |
    },                                         |
    messages: {                                |
        'select': {                            |
            required: "Select a country"--------
        }
    }
});

Check this JSFiddle

1 Comment

@Jeff Though late, glad to help you

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.