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, which is not verbose.
Validation cannot be null display prompt 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 ');
}
ElseIf (! 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.
Create Dom
function createshowelement (CurrentID, ElementID, style, showmsg, tagName) {
if (!tagname) that displays the hint information tagName = ' 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);
}
For the exchange, we welcome the advice and the desire for valuable opinions. Personally feel that even writing a simple script validator, you should also try to follow object-oriented thinking, and pursue a coordinated point in scalability and efficiency that does not affect efficiency and allows us to write any program with a higher scalability, which is not difficult, but is often overlooked by many novice programmers.