简体   繁体   English

我如何验证不使用 jquery 在 textarea 的描述字段中输入手机号码

[英]how can i validate to do not enter mobile number in description field of textarea using jquery

how can i validate to do not enter mobile number in description field of textarea using jquery我如何验证不使用 jquery 在 textarea 的描述字段中输入手机号码

 $('#jobpost').click(function () { var phoneno = /^\\d{10}$/; var discription=$('.nicEdit-main').html(); if(discription.value.match(phoneno)) { alert('gdfgdfg') $("#froala-editor").parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please do not enter Mobile Number</div>"); $('#jobpost_form').attr('onsubmit','return false;'); } });

Try this尝试这个

.html .html

 <div class="container">
    <form id="jobpost_form">
        <div class="row" id="froala-editor">
            <label>Enter Description</label>
            <textarea class="nicEdit-main" placeholder="write about yourself">    
            </textarea>
        </div>
        <button type="submit" id="jobpost">Submit</button>
    </form>
</div>

.Js .js

  $('#jobpost').click((e)=> {
    e.preventDefault();
    var phoneno = /[0-9]{10,12}$/;  // if +91 added it would be 12 
    var discription=$('.nicEdit-main').val();
    if(discription.match(phoneno))
    {
     $("#froala-editor").parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please do not enter Mobile Number</div>");
     $('#jobpost_form').attr('onsubmit','return false;');    
    }
});

Note: You can modify as per your requirement.注意:您可以根据您的要求进行修改。 This given regular expression actually check the length of digits that should be equal or more than 10-12 digits to ensure as a mobile number.这个给定的正则表达式实际上检查应该等于或大于 10-12 位的数字长度以确保为手机号码。

Feel free to comment for more help请随时发表评论以获得更多帮助

if you trying to validate mobile-number in textarea field.如果您尝试在 textarea 字段中验证手机号码。

  • use this pattern /\\d{10}/使用这种模式/\\d{10}/
  • change this $('.nicEdit-main').html();改变这个$('.nicEdit-main').html(); to $('.nicEdit-main').val();$('.nicEdit-main').val();
  • change discription.value.match(phoneno) to this discription.match(phoneno)discription.value.match(phoneno)更改为此discription.match(phoneno)

In the pattern, use ^ and $ only if you want to match only mobile-number.在模式中,仅当您只想匹配手机号码时才使用^$

As per your question you can go through other solution where you can simply restrict user before typing a numeric value in the textarea .根据您的问题,您可以通过其他解决方案,您可以在textarea 中输入数值之前简单地限制用户。

And to do so you can use keypress/input function of textarea(input).为此,您可以使用 textarea(input) 的按键/输入功能。

   <textarea id="txt" oninput="validateInput(event)"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

 $("#txt").keydown(function (e) {if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 || (e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) || (e.keyCode >= 35 && e.keyCode <= 40)) {
            return;
        }
        if (e.key === '!' || e.key === '@' || e.key === '#' || e.key === '$' || e.key === '%' || e.key === '^' || e.key === '*' || e.key === '(' || e.key === ')') {
            return;
        }
        if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
            e.preventDefault();
        }
});



 function validateInput(e) {
            var val = $("#txt").val();
            val = val.replace(/\d+/g, '');
            $("#txt").val(val);
        }

You can check the working demo Right Here .您可以在此处查看工作演示。

Hope this may help you because your question is very unique.希望这可以帮助您,因为您的问题非常独特。

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

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