Javascript and Form validation



  • I found this code while trying to renew it a bit :

     // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        return valid;
    }

    Guess how I changed it ? 



  • @Monkios said:

    I found this code while trying to renew it a bit :

     // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        return valid;
    }

    Guess how I changed it ? 

     

    Hmmm  like this:

     

      // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid==true){valid = valid && checkRadio(frm.ldap)};

       if(valid == true){ return true;}

        return false;
    }



  • @Monkios said:

    I found this code while trying to renew it a bit :

     // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        return valid;
    }

    Guess how I changed it ? 

    Looks like a decent pattern if you have multiple form elements to check...  Consider the processing flow if you add more controls to check...

    // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        if(valid){valid = valid && checkTextBox(frm.txt)};       // Short-Circuits the "checkTextBox" function if "checkRadio" was not valid...
        if(valid){valid = valid && checkCheckBox(frm.chk)};   // more of the same...
        return valid;
    }



  • LOL

     

    function validateForm(frm){


       return checkRadio(frm.ldap) && checkTextBox(frm.txt) && checkCheckBox(frm.chk);

    }



  • @Monkios said:

    Guess how I changed it ? 

    Like this?

    function validateForm(){
        // This function validates the input of the form to ensure there is valid input from the form
        var blnValid = true;
        var objDocument = document;
        var objFormCollection = objDocument.Forms;
        var objFormToValidate = objFormCollection.MyFormName;
        for(var i = 0; i < objFormToValidate.length; i++){
            if(objFormToValidate[i].Name == "ldap"){
                if(blnValid){
                    blnValid = blnValid && checkRadio(objFormToValidate[i]);
                    break;
                }
            }
        }
        return blnValid;
    }



  • I know I know....

    return FileNotFound; 



  • Isn't hand-coded Javascript form validation pretty much a WTF in and of itself these days?


  • Considered Harmful

    @RaspenJho said:

    @Monkios said:

    I found this code while trying to renew it a bit :

     // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        return valid;
    }

    Guess how I changed it ? 

    Looks like a decent pattern if you have multiple form elements to check...  Consider the processing flow if you add more controls to check...

    // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        if(valid){valid = valid && checkTextBox(frm.txt)};       // Short-Circuits the "checkTextBox" function if "checkRadio" was not valid...
        if(valid){valid = valid && checkCheckBox(frm.chk)};   // more of the same...
        return valid;
    }

    The if(valid) parts are superfluous if short circuit logic is used.



  • @BG75 said:

    LOL

     

    function validateForm(frm){


       return checkRadio(frm.ldap) && checkTextBox(frm.txt) && checkCheckBox(frm.chk);

    }

    Good catch, depends on if javascript does short-circuiting or not.



  • @Licky Lindsay said:

    Isn't hand-coded Javascript form validation pretty much a WTF in and of itself these days?

    How so?



  • @RaspenJho said:

    Good catch, depends on if javascript does short-circuiting or not.

     It does.

     

     
     function validateForm(frm){

        if (!checkRadio(frm.ldap)) return false;

        if (!checkTextBox(frm.txt) ) return false;

        if (!checkCheckBox(frm.chk)) return false; 

        return true; 

     }
     



  • @Licky Lindsay said:

    Isn't hand-coded Javascript form validation pretty much a WTF in and of itself these days?

    Some of us eschew your fancy drag & drop editors because they write god-awful, hair-wrenching code that I have to go back and fix? Or maybe I want to do something more indepth than copying/pasting "isEmpty" 20 times?



  • @Volmarias said:

    @Licky Lindsay said:
    Isn't hand-coded Javascript form validation pretty much a WTF in and of itself these days?

    Some of us eschew your fancy drag & drop editors because they write god-awful, hair-wrenching code that I have to go back and fix? Or maybe I want to do something more indepth than copying/pasting "isEmpty" 20 times?

    In frameworks like Struts, you set up the validation rules in a config file on the server, and both client side (javascript) and server-side validation is automatically done by the framework.

    Struts is the lowest common denominator of web app frameworks. I assume that *any* framework you might use that is newer than Struts, will also do this.



     



  • @Monkios said:

    I found this code while trying to renew it a bit :

     // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        return valid;
    }

    Guess how I changed it ? 

    Add at some random point:

      alert("Valid of Field is not, please again.");

     



  • @joe.edwards@imaginuity.com said:

    @RaspenJho said:
    @Monkios said:

    I found this code while trying to renew it a bit :

     // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        return valid;
    }

    Guess how I changed it ? 

    Looks like a decent pattern if you have multiple form elements to check...  Consider the processing flow if you add more controls to check...

    // Form validation
    function validateForm(frm){
        //var valid = true;
        var valid = true;
        if(valid){valid = valid && checkRadio(frm.ldap)};
        if(valid){valid = valid && checkTextBox(frm.txt)};       // Short-Circuits the "checkTextBox" function if "checkRadio" was not valid...
        if(valid){valid = valid && checkCheckBox(frm.chk)};   // more of the same...
        return valid;
    }

    The if(valid) parts are superfluous if short circuit logic is used.

     That's very true, but you could on the other hand look at it like this: the "valid &&" parts are superfluous no matter what!


     


Log in to reply