Search code examples
javascriptlivevalidation

combinate input-fields for Livevalidation


i check some Input Fields with Livevalidation if they are not empty. For this i am using the following code:

        <script type="text/javascript">
            var street = new LiveValidation('street');
            var streetnumber = new LiveValidation('streetnumber');
            street.add( Validate.Presence );
            streetnumber.add( Validate.Presence );
        </script> 

The both input-Filds are inline. But the message will be always directly displayd behind the input-Field. Now, the Message from the first input-Field will Reposition the second Field.

I am looking for the best way to combinate the Validation from 2 Fields and shown only one Message.

here you can see the actual result: http://jsfiddle.net/Vme7C/


Solution

  • http://jsfiddle.net/Vme7C/1/

    var street = new LiveValidation('street',{
        onValid:showmessage(true),onInvalid:showmessage(false)
    });
    var streetnumber = new LiveValidation('streetnumber',{
        onValid:showmessage(true),onInvalid:showmessage(false)
    });
    
    var streetvalid=false,streetnumbervalid=false,
        ls=document.getElementById('msgli');
    function showmessage(val){
        return function(){
            if(this.element.id=="street")
                streetvalid=val;
            else
                streetnumbervalid=val;
            if(streetvalid && streetnumbervalid){
                ls.innerHTML="All fields are valid";
                return;
            }
            if(!streetvalid && !streetnumbervalid){
                ls.innerHTML="No field is valid";
                return;
            }
            ls.innerHTML=(!streetvalid?"Street":"Street number")
                +" field is invalid";
            return;
        }
    }
    
    street.add(Validate.Presence);
    streetnumber.add(Validate.Presence);