开发者

How to display an error if the content of a <textarea></textarea> is empty using javascript?

开发者 https://www.devze.com 2022-12-19 03:00 出处:网络
I know ho开发者_JAVA技巧w to display an error message if the user leaves a required input field blank when filling in a form, but I am not able to do the same with the textarea.i think on change fires

I know ho开发者_JAVA技巧w to display an error message if the user leaves a required input field blank when filling in a form, but I am not able to do the same with the textarea.


i think on change fires for text areas as well so assuming it does

theTextArea.onChange = function(){
  if(this.value == ''){
     alert('You must supply a value for this field');
  }
};

or with jquery

$(document).ready(
  function(){
    $('#mytextarea').change(function(){
      var $this = $(this);
      if($this.val() == ''){
         alert('You must supply a value for this '+$("label[for='mytextarea']").text());
      }
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号