Select "Reject form validation form" (display text)
I am trying to validate a form.
What I am trying to do is submit the form by confirming Option the displayed text is not the parameter value since the parameter values ββare int
Example:
<option value="selectcard">Please select</option>
If the user clicks the submit button, the form should be validated if the option display says Select regardless of the option value.
Code that doesn't work
function fun(){
$('#cardtype').change(function () {
var sel = $('option:selected', this).text();
if (sel == "Please select") {
$('.showotherpDescription').show();
} else {
}
});
}
Doesn't work: http://jsfiddle.net/f5hxpo7g/2/
Also includes a common working example for validating a form based on a parameter value
Checks based on value http://jsfiddle.net/f5hxpo7g/1/
Please let me know what I am doing wrong.
Thanks in advance.
source to share
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/
source to share
I fixed it, you have to 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 that. Also add id="subbutton
.
Here works jsFiddle .
source to share
I checked the jsfiddle you linked and modified it a bit to make it work, you can see it here .
The problem with the code you provided is IMO:
- The problem is with the purpose of your function (validation) and the event you are subscribing to the code (the combination changed event)
- As you get the text from the currently selected parameter, you must create a selector based on the element that contains the parameters, otherwise if your page has more than 1 combo, you will get many values ββ(one for each selected option)
Check the code in the provided jsfiddle 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 {
}
}
}
source to share