I'm trying to set up some form validation for a Gravity Form that I've created. One of the fields that I need to validate is a US ZIP code. I want to pass ZIPs that follow the nnnnn and nnnnn-nnnn patterns. Here's my code:
if ( $field->type == 'address' ) {
$zip = rgar( $value, $field->id . '.5' );
if ( preg_match( "(^(?!0{5})(\d{5})(?!-?0{4})(|-\d{4})?$)", $zip ) && ! $field->get_input_property( '5', 'isHidden' )
) {
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? __( 'Please enter a valid ZIP code (ie. 00000 or 00000-0000).', 'gravityforms' ) : $field->errorMessage;
} else {
$result['is_valid'] = true;
$result['message'] = '';
}
}
My form continues to fail validation and I can't figure out why. I've double checked that .5 is the correct input field number of the ZIP code. Any suggestions?
My form can be found at http://marcusjones.wpengine.com/
shouldn't be easier to use:
/(^\d{5}$)|(^\d{5}-\d{4}$)/
or other function fe:
function isValidPostalCode(postalCode, countryCode) {
switch (countryCode) {
case "US":
postalCodeRegex = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;
break;
default:
postalCodeRegex = /^(?:[A-Z0-9]+([- ]?[A-Z0-9]+)*)?$/;
}
return postalCodeRegex.test(postalCode);
}
and "if" you'll add quite simple.
Related
I've been banging my head all day on this last piece of my project. I'm using Ninja Forms and looking to validate a string before submitting the form. I have broken this into pieces and tested using simple string checks and it worked but when I try to use the file_get_contents() function it's just submitting without running the check. The file realtor_info.txt contains the following: mikeoberdick#gmail.com - BP61AM. The page is found here: https://www.rhwarranty.com/realtor-quote/
add_filter( 'ninja_forms_submit_data', 'my_ninja_forms_submit_data');
function my_ninja_forms_submit_data( $form_data ) {
$form_id = $form_data[ 'form_id' ];
if ( $form_id = 4 ) {
$email_field_id = 41;
$coupon_field_id = 49;
$email = $form_data['fields'][$email_field_id]['value'];
$coupon = $form_data['fields'][$coupon_field_id]['value'];
$string = $email . ' - ' . $coupon;
$file = "https://rhwarranty.com/wp-content/themes/regal-home-warranty/stripe/realtor_info.txt";
//check the file to see if the string exists
if( strpos(file_get_contents($file),$string) !== false) {
return $form_data;
} else {
$form_data['errors']['fields'][$coupon_field_id] = 'Something is not quite right here!';
}
}
}
Any thoughts on what is going wrong?
Why do you assign the value of the form in the if statement ?
Or if ( $form_id = 4 ) {
this should be
if ( $form_id == 4 ) {
I have a filter/validation script that only accepts emails from a certain domain. This is working pretty good but I need to have it on a specific from input ID:
As per https://docs.gravityforms.com/gform_field_validation/#11-email-validation-by-third-party-api#usage :
You can also target a specific field by adding the form id and field
id after the hook name.
//The following declaration targets field 1 in form 6 add_filter(
'gform_field_validation_6_1', 'your_function_name', 10, 4 );
Currently my code looks like this:
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
$result['is_valid'] = true;
$result['message'] = '';
} else {
$result['is_valid'] = false;
$result['message'] = 'E-Mail must be a company.com address';
}
return $result;
}, 10, 4 );
My form and input IDs are confirmed as 1 and 13, respectively:
When I change the code to the following, the script stops working:
add_filter( 'gform_field_validation_13_1', function ( $result, $value, $form, $field ) {
if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
$result['is_valid'] = true;
$result['message'] = '';
} else {
$result['is_valid'] = false;
$result['message'] = 'E-Mail must be a company.com address';
}
return $result;
}, 10, 4 );
Is there another way I need to check my form ID? What is missing here?
From the documentation gform_field_validation_6_1 means that it targets field 1 in form 6.
You form id is 1 and input field id is 13 so the filter should be gform_field_validation_1_13.
I have a form in PHP and if user put correct coupon code something like that is defined:
$ret['status'] = 'success';
and next is:
if( $ret['status'] == 'success' ){
$ret['coupon-id'] = $query->post_id;
$ret['amount'] = $post_option['coupon-discount-amount'];
$ret['type'] = $post_option['coupon-discount-type'];
$discount_text = '';
if( $ret['type'] == 'percent' ){
$discount_text = $post_option['coupon-discount-amount'] . '%';
}else{
$discount_text = gdlr_lms_money_format($post_option['coupon-discount-amount']);
}
$ret['message'] = sprintf(__('You got %s discount', 'gdlr-lms'), $discount_text);
}
And the bottom of the form there is submit button with this conditions:
if( empty($_POST['first_name']) || empty($_POST['last_name']) || empty($_POST['email']) ){
$ret['status'] = 'failed';
$ret['message'] = __('Please fill all required fields.', 'gdlr-lms');
I need to add to this condition that correct coupon is also required. What I need to add to this code to have it done? I have been looking for answers for 5 hours and I tried everything what I have found but nothing work - please help me! :)
This is the website with form - after registration form there will be the entry form to the course I am ask about.
http://semcamp.university/course/starting-company-all-you-need-to-know-to-start-company-2-2-2-2/
You can login in using username: stackoverflow and password: stack1 and valid copuon code is 1234 or test1
Your question is not very clear about in which if statement should the coupon be validated. But, supposing that it needs to be validated in the last one (the if for the submit button), you could do it that way:
if( empty($_POST['first_name']) || empty($_POST['last_name']) || empty($_POST['email']) ){
$ret['status'] = 'failed';
$ret['message'] = __('Please fill all required fields.', 'gdlr-lms');
}
elseif( $ret['status'] != 'success') {
$ret['status'] = 'failed';
$ret['message'] = __('Please provide a valid coupon.', 'gdlr-lms');
}
I am new to wordpress. I have a custom contact form in frontend and i have to validate the data.
Will I have to make validation class or is there any hooks provided by wp.
Regardless of what you use WordPress for, there’s a range of common functions people need their site to perform that aren’t bundled with WordPress. This leaves you with two choices, installing a plugin or creating it yourself.
I am validating form data with my own class wrapper. following are some of the methods you can use :
function handleContactForm() {
if($this->isFormSubmitted() && $this->isNonceSet()) {
if($this->isFormValid()) {
$this->sendContactForm();
} else {
$this->displayContactForm();
}
} else {
$this->displayContactForm();
}
}
public function sendContactForm() {
}
function isNonceSet() {
if( isset( $_POST['nonce_field_for_submit_contact_form'] ) &&
wp_verify_nonce( $_POST['nonce_field_for_submit_contact_form'], 'submit_contact_form' ) ) return true;
else
return false;
}
function isFormValid() {
//Check all mandatory fields are present.
if ( trim( $_POST['contactname'] ) === '' ) {
$error = 'Please enter your name.';
$hasError = true;
} else if (!filter_var($_POST['contactemail'], FILTER_VALIDATE_EMAIL) ) {
$error = 'Please enter a valid email.';
$hasError = true;
} else if ( trim( $_POST['contactcontent'] ) === '' ) {
$error = 'Please enter the content.';
$hasError = true;
}
//Check if any error was detected in validation.
if($hasError == true) {
echo $error;
return false;
}
return true;
}
function isFormSubmitted() {
if( isset( $_POST['submitContactForm'] ) ) return true;
else return false;
}
if you try to use contact form 7 plugin, then you have validation plugin available for this,i.e Jquery Validation For Contact Form 7
or try
http://code-tricks.com/contact-form-7-custom-validation-in-wordpress/
I have the following php code.
Im trying to find out the field name/key that does not exist without using hardcoded static text. Is there a php function that can do this. array_keys_exist is similar to what I want but it only allows for checking of a single key.
ex:
Something like
$keys_to_confirm = ['password','password_old',....];
$user_submitted_input_array = ['password'=>'...', 'somekey'=>'...' ];
bool : all_array_keys_exist($keys_to_confirm,$user_submitted_input_array);
Current code that uses static text to report the missing field/key name
if( isset($input['password']))
{
if( isset($input['password_old'])) )
{
if(isset($input['password_repeat'])) )
{
//good to go!
}
else
{
die('missing form element password_repeat.');
}
}
else
{
die('missing form element password_old.');
}
}
else
{
die('missing form element password.');
}
I had this code but it doesnt say what is missing.
//if the field doesnt exist for some reason lets create a dummy
if( ! isset($input['password'])
OR (!isset($input['password_repeat']))
OR (!isset($input['password_old'])) )
{
die('missing form element.');
}
Try:
foreach( array('password', 'password_repeat', 'password_old') as $key ) {
if( empty($input[ $key ]) ) {
echo 'Missing field: ' . $key;
}
}
You can probably come up with a one liner to do this using array_diff_key, but I think the above is concise enough.
Here's the one liner with array_key_diff. It's not the prettiest but functional.
$keys_to_confirm = array('password' => '','password_old' => '');
$user_submitted_input_array = array('password'=>'...', 'somekey'=>'...');
$validate = array_diff_key($keys_to_confirm, $user_submitted_input_array);
if( empty($validate) ) {
echo 'Passed';
} else {
echo 'Missing field: ' . key($validate);
}
My solution:
if ($field_miss = array_diff_key(array('password', 'password_old', '...'), array_keys($input))) {
echo 'Missing field: ', implode(', ', $field_miss);
}