[英]how to display error message next to textbox in javascript?
I am new to Javascript.我是 Javascript 的新手。 So I had a doubt for the following function, how to display the error message next to text box?所以我对下面的功能有疑问,如何在文本框旁边显示错误信息? right now it is showing a alert message, but I need to change the alert message and need to display it next to textbox?现在它显示一条警报消息,但我需要更改警报消息并需要在文本框旁边显示它?
function AllowLock(){
if (!locker.lock.value.match(/[a-zA-Z]$/) && locker.lock.value !="") {
locker.lock.value="";
alert("Please Enter only valid lock");
}
if(locker.lock.value.length > 5)
alert("max length exceeded");
}
}
you must have any container for that message, so you can create element and then append your message to that, something like this, with jquery您必须有该消息的任何容器,因此您可以创建元素,然后使用 jquery 将消息附加到该元素,就像这样
<input type="text" id="kuku"></input>
$('#kuku').after('<div></div>').html("your message");
You can create a span element next to your textbox您可以在文本框旁边创建一个 span 元素
<input type="text" /><span id="msg"></span>
and put your message.并把你的消息。
document.getElementById("msg").innerHTML = "your message";
Expanding on anna's answer here is a example usage.. this code is the called on the on click of my 'continue' button.在这里扩展安娜的答案是一个示例用法..此代码是在单击我的“继续”按钮时调用的。 Removes all existing error messages at start so they dont stack up etc.在开始时删除所有现有的错误消息,这样它们就不会堆积起来等。
<input id="userZip" type="number">
<input id="userName" type="text">
$('.errorText').remove();
userZip.style.border = "1px solid black";
userName.style.border = "1px solid black";
if(userName.value.length >= 3){
if(userZip.value.length == 5){
//success
loginPanel.style.display = "none";
location.href='#appNavigation/'+'mapNav';
} else {
userZip.style.border = "1px solid red";
$('#userZip').after('<div class="errorText">Zipcode required. 5 numbers</div>');
}
} else {
userName.style.border = "1px solid red";
$('#userName').after('<div class="errorText">Username required. 3 or more characters</div>');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.