[英]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;'); } });
尝试这个
.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
$('#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;');
}
});
注意:您可以根据您的要求进行修改。 这个给定的正则表达式实际上检查应该等于或大于 10-12 位的数字长度以确保为手机号码。
请随时发表评论以获得更多帮助
如果您尝试在 textarea 字段中验证手机号码。
/\\d{10}/
$('.nicEdit-main').html();
到$('.nicEdit-main').val();
discription.value.match(phoneno)
更改为此discription.match(phoneno)
在模式中,仅当您只想匹配手机号码时才使用^
和$
。
根据您的问题,您可以通过其他解决方案,您可以在textarea 中输入数值之前简单地限制用户。
为此,您可以使用 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);
}
您可以在此处查看工作演示。
希望这可以帮助您,因为您的问题非常独特。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.