We can solved this problem by using JavaScript:
Lets say on the html, this is the alert block.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="alertMsg" class="alert alert-warning collapse"> | |
Name required. Please supply! | |
</div> |
By adding return false on the close event, this will prevent the alert from being remove from the DOM. Prior to that we should hide it so that it not be visible on the page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $alertMsg = $("#alertMsg"); | |
$alertMsg.on("close.bs.alert", function () { | |
$alertMsg.hide(); | |
return false; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ($("#Name").val()=="") { | |
$alertMsg.show(); | |
} |
I believe this can be further explain if we have a sample code to play with:
The example below is a form entry that will validate empty entry before posting data to server.
Screen Shot:
index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<title>BootStrap Alert</title> | |
<link href="css/bootstrap.min.css" rel="stylesheet" /> | |
<link href="css/site.css" rel="stylesheet" /> | |
<link href="css/footerBottom.css" rel="stylesheet" /> | |
</head> | |
<body> | |
<div id="wrapper"> | |
<header> | |
<div class="navbar navbar-default" role="navigation"> | |
<div class="container"> | |
<div class="navbar-header"> | |
<button class="btn btn-success navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> | |
<span class="glyphicon glyphicon-align-justify"></span> | |
</button> | |
<a class="navbar-brand" href="#">CodexSquare</a> | |
</div> | |
<div class="navbar-collapse collapse"> | |
<ul class="nav navbar-nav navbar-right"> | |
<li class="nav active"><a href="#">Home</a> | |
</li> | |
<li class="nav"><a href="#">About</a> | |
</li> | |
</ul> | |
</div> | |
</div> | |
</div> | |
</header> | |
<div class="content container"> | |
<div id="alertMsg" class="alert alert-warning collapse box-shadow"> | |
</div> | |
<div class="form-horizontal"> | |
<div class="form-group"> | |
<div class="col-md-2"> | |
<label for="Name" class="control-label">Name</label> | |
</div> | |
<div class="col-md-4 col-sm-6 col-xs-10"> | |
<input type="text" id="Name" class="form-control" placeholder="Your Name" /> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-md-2"> | |
<label for="Address" class="control-label">Addess</label> | |
</div> | |
<div class="col-md-4 col-sm-6 col-xs-10"> | |
<input type="text" id="Address" class="form-control" placeholder="Your Address" /> | |
</div> | |
</div> | |
</div> | |
<hr /> | |
<div> | |
<input type="button" class="btn btn-success" id="submitButton" value="Submit" /> | |
</div> | |
<footer> | |
<div class="container float-left"> | |
<p>© Copyright CodexSquare</p> | |
</div> | |
</footer> | |
</div> | |
<script src="js/jquery-1.8.2.min.js"></script> | |
<script src="js/bootstrap.min.js"></script> | |
<script src="js/site.js"></script> | |
</body> | |
</html> |
site.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
var $alertMsg = $("#alertMsg"); | |
var alertHtmlClose = '<a href="#" data-dismiss="alert" class="close">×</a>'; | |
var alertHtml; | |
$(function () { | |
configListeners(); | |
}); | |
function configListeners() { | |
$("#submitButton").click(function () { | |
submitComments(); | |
}); | |
$alertMsg.on("close.bs.alert", function () { | |
$alertMsg.hide(); | |
return false; | |
}); | |
} | |
function submitComments() { | |
if (!validateInput($("#Name"),"Name is required. Please supply") ) { | |
return false; | |
} | |
if (!validateInput($("#Address"),"Address is required. Please supply") ) { | |
return false; | |
} | |
$alertMsg.hide(); | |
} | |
function validateInput($inputText, message) { | |
if ($inputText.val() == "") { | |
alertHtml = alertHtmlClose; | |
alertHtml = alertHtml.concat(message); | |
$alertMsg.show(); | |
$alertMsg.html(alertHtml); | |
$inputText.focus(); | |
return false; | |
} | |
return true; | |
} | |
})(); |
Thank you! Hope this article helps.
6 comments
Awesome!
ReplyThanks you saved a lot of time
ReplyYou made some good points there. I did a search on the topic and found most people will agree with your blog..
ReplyAlso Visit this site for to learn more about Bootstrap Training
You really did a great job. I found your blog very interesting and very informative. I think your blog is great information source & I like your way of writing and explaining the topics. Keep it up. A bootstrap is a small strap or loop at the back of a leather boot that enables you to pull the entire boot on.In computers, to bootstrap (or “to boot”) is to load a program into a computer using a much smaller initial program to load in the desired program Readmore..
ReplyYou made some good points there. I did a search on the topic and found most people will agree with your blog..
Reply| Certification | Cyber Security Online Training Course|
Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course|
CCNA Training Course in Chennai | Certification | CCNA Online Training Course|
RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai|
SEO Training in Chennai | Certification | SEO Online Training Course
Nice Article, thank you for sharing.
Replylearn power bi