简体   繁体   中英

Custom validation message for a select

I have this select input field where I want to validate its selected field using javascript - and then show a html5 error message (and make its border red) if "01" is selected in the select input field. How can I accomplish this. Right now nothing happens if I select "01". The code can be tried here:

http://jsbin.com/wonuwo/edit?html,css,js,output

<form id="myForm">
<select name="cardexpiremonthPayment" id="cardexpiremonthPayment" title="Du skal vælge den udløbsmåned som optræder på forsiden af dit betalingskort" oninvalid="Please select Accused Name" onchange="checkValid(this)" required>
                                    <option value="" selected="">Vælg måned</option>
                                    <option value="01">01 - Januar</option>
                                    <option value="02">02 - Februar</option>
                                    <option value="03">03 - Marts</option>
                                    <option value="04">04 - April</option>
                                    <option value="05">05 - Maj</option>
                                    <option value="06">06 - Juni</option>
                                    <option value="07">07 - Juli</option>
                                    <option value="08">08 - August</option>
                                    <option value="09">09 - September</option>
                                    <option value="10">10 - Oktober</option>
                                    <option value="11">11 - November</option>
                                    <option value="12">12 - December</option>
                                </select>  

</form>
<script>
    function checkValid(input) {

        if (input.value == "01") {
            input.setCustomValidity("You're having too much fun!");
        } else {
             input.setCustomValidity('');
        }
    }

</script>

Using jQuery ::

$(document).ready(function(){
  $( "#cardexpiremonthPayment" ).change(function() {
    alert($("#cardexpiremonthPayment option:selected").text());
    if($("#cardexpiremonthPayment option:selected").text() == '01 - Januar'){
      alert("Gotcha");
      $(this).css('background','rgba(255, 0, 0, 1)');
    }
  });
});

Hope this is what you expect.

I modyfide your code, so this will work:

 function checkValid(input) { if (input.value == "01") { document.getElementById("myForm").style.border = "5px solid red" ; document.getElementById("error").style.display = ""; } else { document.getElementById("myForm").style.border = "none" ; document.getElementById("error").style.display = "none"; } } 
 <form id="myForm"> <select name="cardexpiremonthPayment" id="cardexpiremonthPayment" title="Du skal vælge den udløbsmåned som optræder på forsiden af dit betalingskort" oninvalid="Please select Accused Name" onchange="checkValid(this)" required> <option value="" selected="">Vælg måned</option> <option value="01">01 - Januar</option> <option value="02">02 - Februar</option> <option value="03">03 - Marts</option> <option value="04">04 - April</option> <option value="05">05 - Maj</option> <option value="06">06 - Juni</option> <option value="07">07 - Juli</option> <option value="08">08 - August</option> <option value="09">09 - September</option> <option value="10">10 - Oktober</option> <option value="11">11 - November</option> <option value="12">12 - December</option> </select> </form> <br/> <div id="error" style="border: 1px solid red; display: none" > You're having too much fun! </div> 

change the html code as this

   <select name="cardexpiremonthPayment" id="cardexpiremonthPayment" title="Du skal vælge den udløbsmåned som optræder på forsiden af dit betalingskort" oninvalid="Please select Accused Name" onchange="checkValid(value)" required>

value must be sent to the js function.you pass the object.It is the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM