First look at the effect, nothing special, hehe!
The code that is invoked is fairly simple and does not need to create additional label or span tags, and the script will automatically generate:
<input type= "text" id= "txt1" onkeyup= "checkresult (this.value = = ', ' txt1 ', ' * here can not be empty! ') '/>
Let's take a look at this checkresult function, the checkcondition parameter represents the judgment condition, and the message is displayed when the condition is true, and the Showafterid parameter is the element ID before the label that is created to display the hint message. Here we create a span after the input to display the hint information, so the parameter value passed in is the id "txt1" of the current input, and the last parameter is the displayed text. Aluminum Forgive?/div>
View Code
Validation cannot be NULL display hint information
function Checkresult (checkcondition, Showafterid, showmsg) {
var showlabelid = Showafterid + "ShowMsg";
if (checkcondition) {
if (document.getElementById (Showlabelid)) {
document.getElementById (showlabelid). InnerHTML = ShowMsg;
} else {
Createshowelement (Showafterid, Showlabelid, "color:red", showmsg, ' span ');
}
else if (!checkcondition) {
if (document.getElementById (showlabelid))
document.getElementById (showlabelid). InnerHTML = ';
}
}
Well, finally, let's take a look at this "createshowelement (CurrentID, ElementID, style, showmsg, TagName)" function: CurrentID is the ID of the current label ElementID the Id;style of the label created for the creation is the style of the label that is created, according to the style; ShowMsg is not spoken; TagName for the label name created, such as label or span.
View Code
// Create a DOM
function createshowelement (CurrentID, ElementID, style, showmsg, tagName) {
if (!tagname) t for presentation information agname = ' label ';
var currentdom = document.getElementById (CurrentID);
var showmsgdom = document.createelement (tagName);
//showmsgdom.setattribute ("style", "Color:" + TextColor + ";");
if (style)
Showmsgdom.setattribute ("style", style);
Showmsgdom.setattribute ("id", elementid);
showmsgdom.innerhtml = showmsg;
CurrentDom.parentNode.insertBefore (Showmsgdom, currentdom.nextsibling);
}