在文本输入键上,我想在没有任何插件的情况下显示验证错误
On text-input keypress I want to show validation error without any plugin
在文本输入键上,我想在没有任何插件的情况下显示验证错误。我正在尝试编写一个通用的jQuery函数,它可以在任何页面中使用。错误弹出窗口应该是动态的。
function checkno(txt,e) {
$(txt).keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
if (!document.getElementById('spanmsg')) {
$(txt).after("<span id='spanmsg' class='fixed'>This is wrong input</span>")
$('#spanmsg').show(0).delay("500").hide(0);
txt.value = '';
}
else {
$('#spanmsg').show(0).delay("500").hide(0);
txt.value = '';
}
return false;
}
});
}
我把整个代码放在下面,
<html>
<head>
<title>Please Rate if it helps</title>
<!-- PUT A PATH OF JQUERY LIBRARY -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script>
<script>
window.onload = function () {
$("#txt").keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
if (!$(txt).next().hasClass("spanmsg")) {
var error = $("<div class='spanmsg'>This is wrong input</div>");
$(txt).after(error);
$(error).css({
top: ($(txt).position().top + 1) + "px",
left: ($(txt).position().left + 1) + "px",
width: $(txt).width() + "px",
height: $(txt).height() + "px"
});
}
$(this).next().show(0).delay("500").delay(500, function () {
$(this).remove();
});
this.value = '';
return false;
}
});
}
</script>
<style>
.spanmsg {
position: absolute;
text-align: center;
color: red;
}
</style>
</head>
<body>
<input type="text" id="txt">
</body>
</html>
如果它对你有帮助,请打分。。。
相反,请尝试keyup方法作为:
$(theElement).keyup(function() {
var $th = $(this);
$th.val( $th.val().replace(/[^0-9]/g, function(str) { alert('You typed " ' + str + ' ".'n'nPlease use only numbers.'); return ''; } ) );
});
希望这能有所帮助。
只使用更新到这一点的代码,没有额外的框架。在JSFiddle上运行。
function checkno(txt,e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
if (!document.getElementById('spanmsg')) {
$(txt).after("<span id='spanmsg' class='fixed'>This is wrong input</span>")
$('#spanmsg').show(0).delay("500").hide(0);
txt.value = '';
}
else {
$('#spanmsg').show(0).delay("500").hide(0);
txt.value = '';
}
return false;
}
}
$('#txt').keyup(function (e) {
checkno(this,e);
});
相关文章:
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 有人知道有没有jquery插件可以在图像上写文本,并让用户将其放置在图像内的任何位置
- Select2js插件无法选择任何选项
- 任何好的 JavaScript Eclipse 插件
- ajaxForm插件在任何单击<按钮>上提交
- 我们是否有任何通用脚本/ jquery插件可以在通过ajax选项卡加载的内容上应用javascript
- Sonar Javascript插件错误:“无法装饰”和“在其源文件夹中不包含任何文件”
- console.log在FF插件生成器中没有显示任何内容
- 如何在没有任何浏览器插件的情况下调试AngularJS绑定{{expression}}
- 火狐插件 - 无法触发任何类型的点击事件
- html5 3D GUI使用,没有任何插件
- 科尔多瓦是否有任何插件可以上传任何扩展名的文件
- 使用ScrollPath jQuery插件,当它击中某个元素时暂停滚动的任何方法
- 使用 jQuery 工具提示插件(或任何其他方法)显示带有 PHP 对象属性的工具提示
- 表中的粘性标题(行)和列,没有任何jQuery插件
- 任何像乔木这样的jquery插件来绘制地图集
- Phonegap/Cordova 2.9自定义插件创建.任何工作示例
- 任何用于为多个页面生成目录的jQuery插件
- 是否有任何jquery插件可以像Facebook墙系统中那样从网络摄像头中捕获图像和视频
- 用于计算Polygon的任何Javascript库或Jquery插件's区域