3

I am trying to validate a form.

What I am trying to do is validate the form by validating the Option display text not the option value, since the option values are int

Example:

<option value="selectcard">Please select</option>

If user clicks submit the form should validate if the option display says Please Select regardless of what the option value is.

Code which is not working

function fun(){
    $('#cardtype').change(function () {
        var sel = $('option:selected', this).text();
        if (sel == "Please select") {
            $('.showotherpDescription').show();
        } else {

        } 
    }); 
}

Not working: http://jsfiddle.net/f5hxpo7g/2/

Also including the regular working example for validating the form based on option value

Validates Based on option value http://jsfiddle.net/f5hxpo7g/1/

Please let me know what i am doing wrong.

Thanks in advance.

3 Answers 3

1

Your function should be like this:

function fun()
{
  var ddl = document.getElementById("cardtype");
  var valor = ddl.options[ddl.selectedIndex].text;
  if (valor == "--- Please select ---")
  {
    alert("Please select a card type");
  }
}

Working fiddle:http://jsfiddle.net/robertrozas/XFtBD/169/

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

3 Comments

thanks.. exactly what i needed. I'll be marking this as my ans :)
Beat me to it :) you can find it with this also : $("select[name='cards'").find('option:selected').text(); From this answer : stackoverflow.com/questions/4641962/…
@user2912702, i'm glad to help...let me know if you need a Jquery based version too...cheers ;)
0

I fixed it, you should use this JS:

$(function () {
    $('#subbutton').click(function () {
        if ($('#cardtype option:selected').text() === "Please select")
        {
            alert("PLEASE SELECT");
        }
    });
});

And remove onclick="..." from the button, you don't need it. Also add id="subbutton.

Here's the working JSFiddle.

Comments

0

I checked out the jsfiddle you referenced and modified it slightly to make it work, you can see it here.

The issue with the code you provided is, IMO:

  1. An issue with the purpose of your function (validation) and the event you're subscribing the code to (the combo selection changed event)
  2. The way you're obtaining the text from the currently selected option, you should create the selector based on the element that contains the options, otherwise, if you have more than 1 combo in your page, you will get lots of values (one for each selected option).

Check out the code in the jsfiddle i provided and let me know if you need more information.

For reference, here is the code i used:

HTML:

<label class="paylabel" for="cardtype">Card Type:</label>
<select id="cardtype" name="cards">
<option value="selectcard">--- Please select ---</option>
<option value="mastercard">Mastercard</option>
<option value="maestro">Maestro</option>
<option value="solo">Solo (UK only)</option>
<option value="visaelectron">Visa Electron</option>
<option value="visadebit">Visa Debit</option>
</select>

<input type="button" onclick="fun()" value="click here">

JS:

function fun(){
    var cmb = document.getElementById('cardtype');
    var sel = cmb.options[cmb.selectedIndex].text;
        if (sel == "--- Please select ---") {
            alert('Please select a different option');
            //$('.showotherpDescription').show();
        } else {

        } 
   } 
}

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.