• Resolved ggedare

    (@ggedare)


    I working on a plugin that enables users to use to two(2) different forms on either a post or page, that is, a custom login form and a application. combined I am looking at 10+ form fields to validate and make sure they are in the right data format (e.g.: text, password length, etc…)

    Currently I am using two(2) separate functions for validation, both using the wp_error function (see code below), that is one for the login form and the other for the registration form.

    //this function validates the login form
    function validatedata($usr, $passwd){
    	$errors = new WP_error();
    	
    	if(empty($usr || $passwd)){
    		$errors->add('required', 'All Fields Required');
    	}
    	
    	if(empty($usr)){
    		$errors->add('usr_required', 'Please Enter Username');
    	}
    	
    	if(empty($passwd)){
    		$errors->add('passwd_required', 'Please Enter Password');
    	}
    	
    	return $errors;
    }
    
    //this function validates the application form
    function validateApp($firstName, $lastName, $phoneNum, $emailAdd){
    	//accessing the WP_Error class via the global variable $appErr
    	$appErr = new WP_Error();
    	
    	if(empty($firstName || $lastName || $phoneNum || $emailAdd)){
    		$appErr->add('required', '*All Fields Required');
    	}
    	
    	if(empty($firstName)){
    		$appErr->add('required', '*First Name Required');
    	}
    	
    	if(empty($lastName)){
    		$appErr->add('required', '*Last Name Required');
    	}
    	
    	if(empty($phoneNum)){
    		$appErr->add('required', '*Phone Number Required');
    	}
    	
    	if(empty($emailAdd)){
    		$appErr->add('required', '*Email Address Required');
    	}
    	
    	if(!empty($emailAdd)){
    		if(!filter_var($emailAdd, FILTER_VALIDATE_EMAIL)){
    			$appErr->add('invalid_format', '*Invalid Email Address');
    		}
    	}
    	
    	/*if(!empty($phoneNum)){
    		if(!filter_var($phoneNum, FILTER_VALIDATE_INT)){
    			$appErr->add('invalid_format', '*Phone Number must be a Number');
    		}
    	}*/
    	return $appErr;
    	
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Validating multiple form fields’ is closed to new replies.