SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,423
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)

    Sharing some code

    I just threw this together at work today for an internal project and figured some of you might find this helpful. This little script just loops through the elements in a form and checks to see if they're changed or blank. If the form is unchanged or blank the submit button is disabled (it is disabled on page load). If any field is changed the submit button is enabled. It's great for very simple forms and I'm sure many of you can modify this to do even more. Just wanted to share. Oh, and if you can improve upon it, feel free to post it here.

    Code:
            //Put the fields you don't want to have validated in here
            var skipFields = new Array("submit");
    
            function disableForm() {
                document.forms[0].submit.disabled = true;
            }
    
            function enableForm() {
                document.forms[0].submit.disabled = false;
            }
    
            function isFormChanged() {
                var isdefault = true;
                var ele = document.forms[0].elements;
                for (i=0; i < ele.length; i++) {
                    if (testField(ele[i])){
                        if (ele[i].type) {
                            if (!isElementEmpty(ele, i)) {
                                isdefault = false;
                            }
                        }
                    }
                }
                if (isdefault)
                    disableForm();
                else
                    enableForm();
            }
    
            function isElementEmpty(ele, i) {
                switch (ele[i].type) {
                    case "text" :
                        if (trim(ele[i].value) == '')
                            return true;
                        break;
                    case "textarea" :
                        if (trim(ele[i].value) == '')
                            return true;
                        break;
                    case "radio" :
                        var radioChkd = true;
                        var tempObj = eval("document.forms[0]." + ele[i].name);
                        for (x = 0; x < tempObj.length; x++){
                            var tempObj2 = eval("tempObj[" + x + "]");
                            if (tempObj2.checked)
                                radioChkd = false;
                        }
                        return radioChkd;
                        break;
                    case "select-one" :
                        if (ele[i].selectedIndex == 0)
                            return true;
                        break;
                    case "select-multiple" :
                        for (var x =0 ; x <ele[i].length; x++) {
                            if (ele[i].options[x].selected == ele[i].options[x].defaultSelected || ele[i].options[x].selected == '')
                                return true;
                        }
                        break;
                    case "checkbox" :
                        if (!ele[i].checked)
                            return true;
                        break;
                    default:
                        return false;
                        break;
                }
            }
    
            function testField(obj){
                var retVal = true;
                for (x=0; x < skipFields.length; x++) {
                    if (obj.name == skipFields[x])
                        retVal = false;
                }
                return retVal;
            }
    
            function trim(inputString) {
                if (typeof inputString != "string")
                    return inputString;
                var retValue = inputString;
                var ch = retValue.substring(0, 1);
                while (ch == " ") {
                    retValue = retValue.substring(1, retValue.length);
                    ch = retValue.substring(0, 1);
                }
                ch = retValue.substring(retValue.length-1, retValue.length);
                while (ch == " ") {
                   retValue = retValue.substring(0, retValue.length-1);
                   ch = retValue.substring(retValue.length-1, retValue.length);
                }
                return retValue;
            }
    
            onload = isFormChanged;

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice work! Very handy script. Thanks for sharing it.

    It's tempting to play around with it - but I don't need to get side-tracked right now

  3. #3
    SitePoint Enthusiast BarbaraStreisand's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Clever script, thanks for sharing sir.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •