drlq92444 2015-12-12 07:37
浏览 60
已采纳

验证表单的textarea - jQuery

I am trying to develope a plugin for an application that let the users invite their friends to use the application by just sending an email. Juts like Dropbox does to let the users invite friends and receive extra space.

I am trying to validate the only field I have in the form (textarea) with JQuery (I am new to JQuery) before submiting it and be handled by php.

This textarea will contain email addresses, separated by commas if more than one. Not even sure if textarea is the best to use for what I am trying to accomplish. Anyway here is my form code:

<form id="colleagues" action="email-sent.php" method="POST">
    <input type="hidden" name="user" value="user" />
    <textarea id="emails" name="emails" value="emails" placeholder="Example: john@mail.com, thiffany@mail.com, scott@mail.com..."></textarea>
    </br><span class="error_message"></span>
    <!-- Submit Button -->
     <div id="collegues_submit">              
        <button type="submit">Submit</button>
     </div>
</form>

Here is what I tried in Jquery with no success:

//handle error 
    $(function() {
          $("#error_message").hide();
          var error_emails = false;

          $("#emails").focusout(function() {
            check_email();
          });

        function check_email() {
          if(your_string.indexOf('@') != -1) {
            $("#error_message").hide();
          } else {
            $("#error_message").html("Invalid email form.Example:john@mail.com, thiffany@mail.com, scott@mail.com...");
            $("#error_message").show();
            error_emails = true;
            }
          }
          
          $("#colleagues").submit(function() {
            error_message = false;
            
            check_email();
            
            if(error_message == false) {
              return true;
            } else {
              return false;
            }
    });

I hope the question was clear enough, if you need more info please let me know.

Many thanks in advance for all your help and advises.

</div>
  • 写回答

5条回答 默认 最新

  • doumengwei0138 2015-12-12 08:02
    关注

    I would use, as I commented above, append() or prepend() and just add fields. As mentioned in another post, client side use jQuery validation, but you should for sure validate server-side using a loop and filter_var($email, FILTER_VALIDATE_EMAIL). Here is a really basic example of the prepend():

    <form id="colleagues" action="" method="POST">
        <input type="hidden" name="user" value="user" />
        <input name="emails[]" id="starter" placeholder="Email address" />
        <div id="addEmail">+</div>
        </br><span class="error_message"></span>
        <!-- Submit Button -->
         <div id="collegues_submit">                
            <button type="submit">Submit</button>
         </div>
    </form>
    <script>
    $(document).ready(function() {
        $("#addEmail").click(function() {
            $("#colleagues").prepend('<input name="emails[]" placeholder="Email address" />');
        });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流