admin管理员组文章数量:1279188
I'm trying to show error messages in div
container using jQuery for validation.
So, instead of an alert message, I want to show the error message after every control where ever the validation fails.
if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
var errName = document.getElementByID("name");
errName.innerHTML += "Please enter name";
errName.innerHTML += ".red {color:red;}";
document.getElementByID("name").val = errName;
Any suggestions?
I'm trying to show error messages in div
container using jQuery for validation.
So, instead of an alert message, I want to show the error message after every control where ever the validation fails.
if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
var errName = document.getElementByID("name");
errName.innerHTML += "Please enter name";
errName.innerHTML += ".red {color:red;}";
document.getElementByID("name").val = errName;
Any suggestions?
Share Improve this question edited Mar 10, 2015 at 3:17 AstroCB 12.4k20 gold badges59 silver badges74 bronze badges asked Mar 10, 2015 at 3:05 Harsh SinghiHarsh Singhi 1172 gold badges5 silver badges14 bronze badges 3- Use a jquery validation plugin. – RaviH Commented Mar 10, 2015 at 3:23
- where issue we will be suggesting? – Nin-ya Commented Mar 10, 2015 at 3:30
- Actually, I am new to Jquery. Any ways ,I will try doing that. Thanks... – Harsh Singhi Commented Mar 10, 2015 at 6:45
2 Answers
Reset to default 7A more pure jQuery approach would be as shown below
jQuery Code
if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
var errName = $("#name"); //Element selector
errName.html("Please enter name"); // Put the message content inside div
errName.addClass('error-msg'); //add a class to the element
}
CSS:
.error-msg{
background-color: #FF0000;
}
Update:
You can even bine the jQuery methods on any selector. Whenever we apply a jQuery menthod on any selector, it returns a "this" pointer, so we can bine multiple methods and apply them to a selector using a single statement. This is called "chaining"
Read more here: http://www.w3schools./jquery/jquery_chaining.asp
if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
$("#name").html("Please enter name")
.addClass("error-msg"); // chained methods
}
if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
var errName = $("#name"); //get element by ID
errName.append("Please enter name"); //append information to #name
errName.attr("style", "background-color: red;"); //add a style attribute
}
Edit: or you could do it like so:
if (name == '' || email == '' || mobile == '' || password == '' || cpassword == '') {
var errName = $("#name"); //get element by ID
errName.append("Please enter name"); //append information to #name
errName.attr("class", "alert"); //add a class to the element
}
Then you will have an .alert
class. This makes it possible for you to use this class in your CSS file.
.alert {
background-color: #FF0000;
}
本文标签: javascriptShow error messages in div container for validations using JqueryStack Overflow
版权声明:本文标题:javascript - Show error messages in div container for validations using Jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741277210a2369799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论