Creating common form validation using native javascript: Sharper use of DOM objects

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, 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.

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.