Title: Validating multiple form fields
Last modified: October 19, 2018

---

# Validating multiple form fields

 *  Resolved [ggedare](https://wordpress.org/support/users/ggedare/)
 * (@ggedare)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/validating-multiple-form-fields/)
 * 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)

 *  [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * (@howdy_mcgee)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/validating-multiple-form-fields/#post-10796727)
 * I’m not sure what the question is here. What issue are you having? How are you
   submitting the form?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/validating-multiple-form-fields/#post-10798314)
 * The more specific validation is to a particular field, the better. If one function
   is validating a wide variety of data types, you are likely letting poor data 
   get through. You need to also sanitize your data. A separate, but related process.
   One sanitation function can likely sanitize many different fields. You can learn
   more about this on these pages:
    [Validating Sanitizing and Escaping User Data](https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data)
   [Data Validation](https://codex.wordpress.org/Data_Validation) [https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/](https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/)
   [https://developer.wordpress.org/plugins/security/securing-input/](https://developer.wordpress.org/plugins/security/securing-input/)

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Validating multiple form fields’ is closed to new replies.

## Tags

 * [data validation](https://wordpress.org/support/topic-tag/data-validation/)
 * [plugin-development](https://wordpress.org/support/topic-tag/plugin-development/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/validating-multiple-form-fields/#post-10798314)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
