使用Javascript在enter上验证textarea
我找到了以下用于执行此操作的JQUERY脚本:使用Javascript在enter上验证textarea,javascript,jquery,Javascript,Jquery,我找到了以下用于执行此操作的JQUERY脚本: $(document).ready(function(){ $('textarea').on('keypress', function(e) { var val = $('textarea').val(); var validaterror = document.getElementById('errorvalidate'); if (e.which == 13) { if(! /\S/.test(val)) {
$(document).ready(function(){
$('textarea').on('keypress', function(e) {
var val = $('textarea').val();
var validaterror = document.getElementById('errorvalidate');
if (e.which == 13) {
if(! /\S/.test(val)) {
validaterror.textContent = 'Please enter domain names in the field.';
return false;
}
validaterror.textContent = '';
}
});
});
但是我不想在我的网站上使用Jquery,因为我对页面速度很着迷,这是我网站上使用Jquery并希望将Jquery转换为Javascript的单一脚本。请帮我解决这件事。试试这样的办法 JAVASCRIPT
function validateMe(obj){
var val = obj.value;
var validaterror = document.getElementById('errorvalidate');
if (e.which == 13) {
if(! /\S/.test(val)) {
validaterror.textContent = 'Please enter domain names in the field.';
return false;
}
validaterror.textContent = '';
}
}
HTML代码
<textarea rows="10" cols="5" onkeypress="validateMe(this)"></textarea>
好吧,你可以一点一点地替换它:
$(document).ready(function(){
我想:
if (document.readyState === "complete") // jQuery uses an interval to check this.
document.getElementById("your_textarea").onkeydown = function(evt) {
// do what you like
};
document.getElementsByTagname('textarea')[0].value // or give it an id and use getElementById
及
我想:
if (document.readyState === "complete") // jQuery uses an interval to check this.
document.getElementById("your_textarea").onkeydown = function(evt) {
// do what you like
};
document.getElementsByTagname('textarea')[0].value // or give it an id and use getElementById
及
我想:
if (document.readyState === "complete") // jQuery uses an interval to check this.
document.getElementById("your_textarea").onkeydown = function(evt) {
// do what you like
};
document.getElementsByTagname('textarea')[0].value // or give it an id and use getElementById
试试这个
脚本
document.getElementById('textId').onkeypress = function (e) {
var val = this.value;
var validaterror = document.getElementById('errorvalidate');
if (e.which == 13) {
if (!/\S/.test(val)) {
validaterror.innerHTML = 'Please enter domain names in the field.';
return false;
}
validaterror.innerHTML = '';
}
}
HTML
<textarea id="textId"></textarea>
<div id='errorvalidate'></div>
首先处理事件
var handleEvent = function(_obj, _execute, _eventName) {
// Check for browser support of event handling capability
if (_obj.addEventListener)
_obj.addEventListener(_eventName, _execute, false);
else if (_obj.attachEvent)
_obj.attachEvent('on'+_eventName, _execute);
else
_obj['on'+_eventName] = _execute;
}
验证内容
var validate = function (){
var obj = document.getElementById('textarea');
var val = obj.value;
var validaterror = document.getElementById('errorvalidate');
if (e.which == 13) {
if(! /\S/.test(val)) {
validaterror.textContent = 'Please enter domain names in the field.';
return false;
}
validaterror.textContent = '';
}
}
把一切放在一起:
var textarea = document.getElementById('textarea');
handleEvent(textarea, validate, 'keypress');
@Teemu这是我已经拥有的代码,它正在工作,但它在Jquery中,希望它将其转换为javascript.1。
函数验证
中有一个输入错误,我写了函数
。2.您至少应该为textarea元素定义一个id。现在,我尝试更正您的JSFIDLE脚本,请看以下内容:它在回显消息,但没有返回false,它将转到下一行。检查它,因为它与正则表达式不匹配。现在还不清楚您真正想要验证什么。我想在用户点击enter时进行验证,以避免让他转到另一行并回显消息。只需在文本区域中点击回车键。