JavaScript creates common form validation code

Source: Internet
Author: User
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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.