changing the language of error message in required field in html5 contact form

后端 未结 9 879

I am trying to change the language of the error message in the html5 form field.

I have this code:



        
相关标签:
9条回答
  • 2020-11-27 13:31

    I know this is an old post but i want to share my experience.

    HTML:

    <input type="text" placeholder="Username or E-Mail" required data-required-message="E-Mail or Username is Required!">
    

    Javascript (jQuery):

    $('input[required]').on('invalid', function() {
        this.setCustomValidity($(this).data("required-message"));
    });
    

    This is a very simple sample. I hope this can help to anyone.

    0 讨论(0)
  • 2020-11-27 13:33

    your forget this in oninvalid, change your code with this:

        oninvalid="this.setCustomValidity('Lütfen işaretli yerleri doldurunuz')"
    

    <form><input type="text" name="company_name"  oninvalid="this.setCustomValidity('Lütfen işaretli yerleri doldurunuz')" required /><input type="submit">
    </form>

    0 讨论(0)
  • 2020-11-27 13:36
    <input type="text" id="inputName"  placeholder="Enter name"  required  oninvalid="this.setCustomValidity('Please Enter your first name')" >
    

    this can help you even more better, Fast, Convenient & Easiest.

    0 讨论(0)
  • 2020-11-27 13:43

    setCustomValidity's purpose is not just to set the validation message, it itself marks the field as invalid. It allows you to write custom validation checks which aren't natively supported.

    You have two possible ways to set a custom message, an easy one that does not involve Javascript and one that does.

    The easiest way is to simply use the title attribute on the input element - its content is displayed together with the standard browser message.

    <input type="text" required title="Lütfen işaretli yerleri doldurunuz" />
    

    enter image description here

    If you want only your custom message to be displayed, a bit of Javascript is required. I have provided both examples for you in this fiddle.

    0 讨论(0)
  • 2020-11-27 13:45

    This work for me.

    <input oninvalid="this.setCustomValidity('custom text on invalid')" onchange="this.setCustomValidity('')" required>
    

    onchange is a must!

    0 讨论(0)
  • 2020-11-27 13:46
    //Dynamic custome validation on all fields
    //add validate-msg attr to all inputs
    //add this js code
    
    $("form :input").each(function(){
                        var input = $(this);
                        var msg   = input.attr('validate-msg');
                        input.on('change invalid input', function(){
                            input[0].setCustomValidity('');
                            if(!(input[0].validity.tooLong || input[0].validity.tooShort)){
                                if (! input[0].validity.valid) {
                                    input[0].setCustomValidity(msg);
                                }
                            }
    
    
                        });
                    });
    
    0 讨论(0)
提交回复
热议问题