简体   繁体   English

文本区域验证中的最小长度

[英]Minimal length in textarea validation

I have a problem with my script. 我的脚本有问题。 It is only partly working. 它只是部分起作用。 If i enter less then 15 charachters the alert appears but then i click ok on the alert massage and the from gets send anyway. 如果我输入的字符少于15个字符,则会出现警报,但是我在警报消息上单击“确定”,无论如何都会从中发送消息。 I am not sure waht i'm doing wrong. 我不确定我做错了什么。 Here is my script: 这是我的脚本:

function checktextarea() {
var minLength = 15;
var $textarea = $('#massage');
if($textarea.text().split(/\s+/).length < minLength) {
  alert('You need to enter at least ' + minLength + ' words');
    return false;
 }
}

This is the html: 这是html:

<form action="kontaktsi.php" name="myForm" id="myForm" method="post" class="contact_form" onsubmit="checktextarea()">
 <span class="sporo">
  <input type="text" id="fname" name="fname" class="contacttextform form-control" placeholder="Name" required>
 </span>
 <input type="email" id="email" name="email" class="contacttextform" placeholder="Your email" required><br><br>
 <textarea name="message" id="message" cols="8" rows="8" class="contacttextarea" placeholder="text text text?" required></textarea>
 <br>
 <div class="send">
  <input name="send" type="submit" class="contactformbutton" style="width:150px;" value="Send">
 </div>
</form>

change your <form> tag into this: 将您的<form>标记更改为此:

<form action="kontaktsi.php" ... method="post" onsubmit="return checktextarea()">

You need to add return to the call, in order to pass the boolean value false to the submit event. 您需要将return添加到调用中,以便将布尔值false传递给Submit事件。

There's also a typo in your script: change $('#massage') into $('#message') Finally, you need to use val() instead of text() to get the value of a <textarea> . 脚本中还有一个错字:将$('#massage')更改为$('#message')最后,您需要使用val()而不是text()来获取<textarea>的值。 Here's the final script: 这是最终的脚本:

function checktextarea() {
    var minLength = 15;
    var textarea = $('#message');
    if(textarea.val().replace(' ') < minLength) {
        alert('You need to enter at least ' + minLength + ' words');
        return false;
    }
    return true;
}

I am not 100% sure if it is the only way, but the last time I solved this problem I avoided the generic onsubmit mechanism; 我不能百分百确定这是否是唯一的方法,但是上次我解决此问题时,我避免了通用的onsubmit机制。 precisely because of the missing way of breaking in case of error. 正是由于缺少错误情况下的中断方式。

Instead, one can bind a jQuery submit event and then use preventDefault() in case of error, as described here: 相反,可以绑定一个jQuery submit事件,然后在发生错误的情况下使用preventDefault() ,如下所述:

http://api.jquery.com/submit/ http://api.jquery.com/submit/

One can even submit directly with jQuery: Submit a form using jQuery 甚至可以直接使用jQuery 提交:使用jQuery提交表单

It is slightly more work, but you have much better control. 它的工作量稍大一些,但是您拥有更好的控制权。

try this 尝试这个

function checktextarea() {
  var minLength = 15;
  var $textarea = $('#massage');
  if($textarea.val().split(' ').length < minLength) {
    alert('You need to enter at least ' + minLength + ' words');
    return false;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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