Javascript Form Validation Help

Hey Guys,

I am using liveValidation and it works great although i’m just trying to add an IF statement to it, but i can’t seem to get it to work.

Here is my JS:
[JS]
var firstname = new LiveValidation(‘firstname’, { validMessage: ’ ', wait: 750} );
firstname.add( Validate.Presence, { failureMessage: “Please Complete” } );

        var lastname = new LiveValidation('lastname', { validMessage: ' ', wait: 750} );
        lastname.add( Validate.Presence, { failureMessage: "Please Complete" } );
        
        if (document.form.email == "" || document.form.fax == "") {
        
            var email = new LiveValidation('email', { validMessage: ' ', wait: 750} );
            email.add( Validate.Presence, {failureMessage: "Please enter an e-mail address" } );
            email.add( Validate.Email, {failureMessage: "Please enter a correct e-mail address" } ); 
                       
            var fax = new LiveValidation('fax');
            fax.add( Validate.Numericality );
            fax.add( Validate.Presence, {failureMessage: "Please Complete" } );
        
        } else if (document.form.email != "" || document.form.fax != "") {
            
            var email = new LiveValidation('email', { validMessage: ' ', wait: 750} );
            email.add( Validate.Email, {failureMessage: "Please enter a correct e-mail address" } ); 
                       
            var fax = new LiveValidation('fax');
            fax.add( Validate.Numericality );
                
        }

[/JS]

Any ideas why it won’t work??
All the other ones work except for those last two.

Thanks in advance!
:smiley:

Try just using else, not else if. Just a thought

tried that but they all stopped working then :frowning:

Thanks - any other ideas??

Could be that the form hasn’t loaded when you are referencing values within it. Try putting from the if statement on into a function and calling the function in the onfocus event of the form.

Ok my else if statement is working correctly just not the else statement.

Here is my updated JS

[AS]
var firstname = new LiveValidation(‘firstname’, { validMessage: ’ ', wait: 750} );
firstname.add( Validate.Presence, { failureMessage: “Please Complete” } );

        var lastname = new LiveValidation('lastname', { validMessage: ' ', wait: 750} );
        lastname.add( Validate.Presence, { failureMessage: "Please Complete" } );
		
		function validate(){
        
			if (email == "" || fax == "") {
			
				var email = new LiveValidation('email', { validMessage: ' ', wait: 750} );
				email.add( Validate.Presence, {failureMessage: "Please enter an e-mail address" } );
				email.add( Validate.Email, {failureMessage: "Please enter a correct e-mail address" } ); 
						   
				var fax = new LiveValidation('fax');
				fax.add( Validate.Presence, {failureMessage: "Please Complete" } );
				fax.add( Validate.Numericality );
			
			} else if (email != "" || fax != "") {
				
				var email = new LiveValidation('email', { validMessage: ' ', wait: 750} );
				email.add( Validate.Email, {failureMessage: "Please enter a correct e-mail address" } ); 
						   
				var fax = new LiveValidation('fax');
				fax.add( Validate.Numericality );
					
			}
		
		}
		
		validate();

[/AS]

got it working i was using “” instead of null!!

:smiley:

Right on! :beer: